NaN
NaN

Reputation: 691

How can I organize a list of fields based on another field?

I am trying to show a list of items organized by their category, with their respective category name directly above it in bold. For example:

Category 1

Category 2

However, here is what the code currently does:

Category 1

Category 2

The two model classes involved taken from /models.py:

class Model(models.Model):
    model_number = models.CharField('Model Number', max_length = 50)
    manufacturer = models.ForeignKey('Manufacturer', on_delete = models.SET_NULL, null = True)
    category = models.ForeignKey('Category', on_delete = models.SET_NULL, null = True)
    description = models.TextField(max_length = 1000, help_text = "Enter brief description of product", null = True) #TextField for longer descriptions

    def __str__(self):
        return f'{self.model_number}..........{self.description}'

    def get_absolute_url(self):
        #Returns the url to access a particular location instance
        return reverse('model-detail', args=[str(self.id)])

class Category(models.Model):
    category = models.CharField('Equipment Type', max_length = 50, 
    help_text = "Enter general category of equipment")

    class Meta:
        verbose_name_plural = "categories" #default would have shown as "Categorys" on admin page

    def __str__(self):
        return self.category

/model_list.html

{% extends "base_generic.html" %}

{% block content %}
    <div style="margin-left:20px; margin-top:20px">
      <h1>Model list</h1>
    </div>
    {% if model_list %}

      {% for category in model_list %}
        <li><strong>{{ category.category }}</strong></li>
          <ul>
            {% for model in model_list %}
              <li>
              <a href="{{ model.get_absolute_url }}"> {{ model.model_number }}..........{{ model.description }} </a>
              </li>
            {% endfor %}
          </ul>
      {% endfor %}
    {% else %}
        <p>There is no equipment in the database</p>
    {% endif %}


{% endblock %}

Upvotes: 2

Views: 122

Answers (1)

user9245558
user9245558

Reputation:

For ForeignKey fields you can access one "Model"'s attributes from another's:

{% for model in models %}
    {{ model.category.category }}
    {{ model.category.help_text }}
{% endfor %}

Essentially - think of the field's for manufacturer and category that you have above in your Model as fields that are storing objects - once you get that field - your variable isn't a string or a integer, but it's an instance of the Category model. Then you can begin to select the attributes for that model, that is to say model.category is an instance of the Category object.

It looks like, from the way you have organised your models, that what you want to achieve is a little tricky. I would maybe advise looking at folding your categorisation of all your models in an array of objects of objects:

in views.py:

models = [ { 'category': 'categoryName', 'objects': [ SomeModelInstance1 , ... ] } ]

Then:

{% for model in models %}
    model.category
    {% for object in model.objects.all %}
         {{ object.name }}
    {% endfor %}
{% endfor %}

Let me know if this points you in the right direction...happy to help in the comments.

Upvotes: 1

Related Questions