Django Crispy Forms showing related foreign key values

I have an order form that displays the available items that are bound to a Catalog object. Instead of displaying available items, currently only the name of the available field is displayed instead of the available items. Is there a way to tweak the values ​​in a crispy way? Similar to how it is done in a template, for example:

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

      

Here is my form:

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')

            )

        )

      

Here is my model:

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')

      

This is what I am currently returning to the template: (this is just the name of the products field in the Catalog) enter image description here

+3


source to share





All Articles