12

I'm trying to find a solution to my problem.

models.py

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()
    description = models.TextField()

def archive_quality(self):
    return self.archive_set.order_by('-quality').distinct().values_list('quality', flat=True)


class Archive(models.Model):
    CHOICES_QUALITY = (
        ('1', 'HD YB'),
        ('2', 'HD BJ'),
        ('3', 'HD POQD'),
        ('4', 'HD ANBC'),
    )
    article = models.ForeignKey(Article)
    quality = models.CharField(max_length=100, choices=CHOICES_QUALITY)

arhives.html

{% for article in articles %}
    {{ article }}
    {% for quality in article.archive_quality %}
        {{ quality.get_quality_display }}#This is not working
    {% endfor %}
{% endfor %}

Update The function archive_quality is important, because it prevents recurrence in the template objects.

Example:
article:
   My article one
Archive:
       quality: 1111222333 >> without the function
       quality: 123 >> with function
doniyor
  • 36,596
  • 57
  • 175
  • 260
Soy Latam
  • 182
  • 1
  • 2
  • 10

2 Answers2

11

Option #1:

models.py

CHOICES_QUALITY = (
    ('1', 'HD YB'),
    ('2', 'HD BJ'),
    ('3', 'HD POQD'),
    ('4', 'HD ANBC'),
)

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField()
    description = models.TextField()

    def archive_quality(self):
        quality = self.archive_set.order_by('-quality').distinct().values_list(
            'quality', flat=True)
        lists = []
        for q in quality:
            for choice in CHOICES_QUALITY:
                if choice[0] == q:
                    lists.append({'quality': choice[1]})
        return lists

class Archive(models.Model):
    article = models.ForeignKey(Article)
    quality = models.CharField(max_length=100, choices=CHOICES_QUALITY)

template

{% for article in articles %}
    {% for item in article.archive_quality %}
        {{ item.quality }},
    {% endfor %}
{% endfor %}

Option #2:

archive_tag.py

from django import template
from app_name.models import CHOICES_QUALITY

register = template.Library()

@register.filter
def quality(q):
    for choice in CHOICES_QUALITY:
        if choice[0] == q:
            return choice[1]
    return ''

template

{% load archive_tag %}

{% for article in articles %}
    {% for item in article.archive_quality %}
        {{ item|quality }},
    {% endfor %}
{% endfor %}
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
catherine
  • 22,492
  • 12
  • 61
  • 85
7

Try: get_quality_display()

ref: https://docs.djangoproject.com/en/4.1/ref/models/instances/#django.db.models.Model.get_FOO_display

Dhinesh Kumar
  • 119
  • 1
  • 6
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32561004) – pigrammer Aug 29 '22 at 15:30