Ivan Lopez
Ivan Lopez

Reputation: 98

Fetch Django models in list view to show both

I have this models:

Article:

class Article(models.Model):
    sku = models.CharField(
        max_length=5,
        primary_key=True,
        validators=[MinLengthValidator(5)]
    )
    ean = models.CharField(
        max_length=13,
        unique=True,
        validators=[MinLengthValidator(13)]
    )
    parent = models.ForeignKey(
        'Article',
        related_name='children',
        null=True,
        blank=True,
        on_delete=models.PROTECT
    )
...

and ArticleTranslations:

class ArticleTranslation(models.Model):
    LANGUAGE_CHOICES = (
        (settings.ENGLISH, _("english")),
        (settings.FRENCH, _("french")),
        (settings.GERMAN, _("german")),
        (settings.ITALIAN, _("italian")),
        (settings.PORTUGUESE, _("portuguese")),
        (settings.SPANISH, _("spanish")),
    )
    article = models.ForeignKey(
        'Article',
        on_delete=models.CASCADE,
        related_name='translations',
    )

I need to return both, combined to show the 2 items in a Django Templates HTML for. Need to have the item and the foreign key and see it in the same iterations. I wanna do it with the methods of the class List View and django, and not doing something bad. I have this ListView:

    def get_queryset(self):
        queryset = (Article.objects
                    .all()
                    .prefetch_related('translations')
                    .order_by('-sku'))

        print(queryset)

        return queryset

And in my HTML, need to show the foreign key values:

{% extends "base.html" %}

{% block content %}

    {% for article in article_list %}
        <div class="container">
            <h2>{{ article.translations.NEED_TO_SHOW_THAT_PARAMETER }}</h2>
        </div>
    {% endfor %}


{% endblock content %}

Upvotes: 0

Views: 170

Answers (1)

xxbinxx
xxbinxx

Reputation: 1553

In your view you're already getting right data. So in your template you can simply loop through the Article objects and it's foreign key values (as they can be accessed using related_name)

you're using {% for article in article_list %} so I believe you have context_object_name = 'article_list' set in your views.

I'm guessing extra fields for translations model which you're probably wanting to access. If your model looks something like this:

class ArticleTranslation(models.Model):
    LANGUAGE_CHOICES = (
        (settings.ENGLISH, _("english")),
        (settings.FRENCH, _("french")),
        (settings.GERMAN, _("german")),
        (settings.ITALIAN, _("italian")),
        (settings.PORTUGUESE, _("portuguese")),
        (settings.SPANISH, _("spanish")),
    )
    article = models.ForeignKey(
        'Article',
        on_delete=models.CASCADE,
        related_name='translations',
    )
    language = models.CharField(max_length=10, choices=LANGUAGE_CHOICES)

Then in your template you can do:

{% extends "base.html" %}

{% block content %}
    {% for article in article_list %}
        <div class="container">
            {% for object in article.translations.all %}
                <h2>Chosend language - {{ object.language }}</h2>
            {% endfor %}
        </div>
    {% endfor %}

{% endblock content %}

Explanation: article.translations.all will give you all translations for one article, using this you can get the field value

Upvotes: 1

Related Questions