Django Crispy Forms显示外键的相关值

时间:2014-10-26 19:49:50

标签: django django-forms django-crispy-forms

我有一个订单表格,显示附加到Catalog对象的可用项目。它不是显示可用的项目,而是显示可用的字段名称而不是可用的项目。有没有办法以酥脆的形式定位价值?类似于在模板中完成的操作,如:

{% for i in catalog.annual_products.all %}

这是我的表格:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, ButtonHolder, Submit
from . import models

class OrderListForm(forms.ModelForm):
    class Meta:
        fields = ('order_lines',)
        model = models.Order



    def __init__(self, *args, **kwargs):
        super(OrderListForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'order_lines',
            ButtonHolder(
                Submit('create', 'Create')

            )

        )

这是我的模特:

class Catalog(models.Model):
products = models.CharField(max_length=200)

def __unicode__(self):
    return self.products

class Issue(models.Model):
    catalog = models.ForeignKey(Catalog, related_name='issue_products')
    Volume = models.DecimalField(max_digits=3, decimal_places=1)

    def __unicode__(self):
        return unicode(self.catalog)

class Annual(models.Model):
    catalog = models.ForeignKey(Catalog, related_name='annual_products')
    year_id = models.IntegerField(max_length=4)
    start_date = models.CharField(max_length=6)
    end_date = models.CharField(max_length=6)
    def __unicode__(self):
        return unicode(self.year_id)


class Annual_Issue(models.Model):
    annual_id = models.ForeignKey(Annual, related_name='annual_ids')
    issue_id = models.ForeignKey(Issue, related_name='issues')
    def __unicode__(self):
        return self.annual_id


class Article(models.Model):
    catalog = models.ForeignKey(Catalog, related_name='article_products')
    title = models.CharField(max_length=200)
    abstract = models.TextField(max_length=1000, blank=True)
    full_text = models.TextField(blank=True)
    proquest_link = models.CharField(max_length=200, blank=True, null=True)
    ebsco_link = models.CharField(max_length=200, blank=True, null=True)

    def __unicode__(self):
        return self.title


class Order(models.Model):
    user = models.ForeignKey(User, related_name='who_ordered')
    order_lines = models.ForeignKey(Catalog, related_name='items_ordered')

这是我目前在模板中返回的内容:(这只是Catalog中产品字段的名称) enter image description here

0 个答案:

没有答案