Thanks for the suggestions, I will change the name from Model after I can 
get this working. 

So, I've changed the code using your help to:

  {% if category_list %}
    {% for category in category_list %}
        <li><strong>{{ category }}</strong></li>
          <ul>
            {% for model in category.model_set %}
              <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 %}

However, this just prints "There is no equipment in the database". Am I 
missing something? I will try to experiment a little more. 

On Monday, August 27, 2018 at 9:52:56 AM UTC-4, Matthew Pava wrote:
>
> Hi Jay,
>
> Firstly, I would avoid calling a model “Model.”  Maybe “Product” would be 
> better?  It’s only because of Django’s models.Model class.  That will 
> likely cause confusion in the future for you.
>
>  
>
> You’ll want to work with the Category model primarily and use a reverse 
> lookup to get to the corresponding “Model” instead of working with a 
> model_list.  The reverse lookup of category to model is model_set by 
> default.  You can change the name if you want.
>
>  
>
> {% for category in category_list %}
>
>       <strong>{{ category }}</strong>
>
>       <ul>
>
>       {% for model in category.model_set %}
>
>             <li><a href="{{ model.get_absolute_url }}">{{ model }}</a></li>
>
>       {% endfor %}
>
>       </ul>
>
> {% empty %}
>
>       <p>There is no equipment in the database with a category.</p>
>
> {% endfor %}
>
>  
>
>  
>
> Check out 
> https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_one/
>
>  
>
>  
>
> *From:* [email protected] <javascript:> [mailto:
> [email protected] <javascript:>] *On Behalf Of *Jay
> *Sent:* Monday, August 27, 2018 8:02 AM
> *To:* Django users
> *Subject:* Printing model fields organized by category
>
>  
>
> I have a Class called "Model" that has a field called "model_numbers". I 
> have another class called "Category" that has a field called "category". In 
> the Model Class, I have a field called category that is a Foreign Key to 
> the field category in the Class category.
>
>  
>
> [image: Screen Shot 2018-08-27 at 8.58.54 AM.png]
>
>  
>
> Also, here is the html that prints the modeul_number and category fields:
>
>  
>
> [image: Screen Shot 2018-08-27 at 9.01.10 AM.png]
>
>  
>
>  
>
> What the code is printing:
>
>  
>
> CATEGORY 1:
>
>  - Field 1
>
>  - Field 2
>
>  - Field 3 
>
>  
>
> CATEGORY 2:
>
>  - Field 1
>
>  - Field 2
>
>  - Field 3
>
>  
>
> What I want it to print is:
>
>  
>
> CATEGORY 1:
>
>  - Field 1
>
>  - Field 2
>
>  
>
> CATEGORY 2:
>
> - Field 3
>
>  
>
> In this way it prints out all the model numbers under their respective 
> category name (instead of all model numbers under every category, which is 
> not correct as each model number only has one category). 
>
>  
>
> I posted about this on stackoverflow, and received a response but I am 
> still confused. 
> <https://stackoverflow.com/questions/51882942/how-can-i-organize-a-list-of-fields-based-on-another-field>
>
>  
>
> Thank you!
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To post to this group, send email to [email protected] 
> <javascript:>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/098cc4aa-374a-4e9a-9da1-ee49bab591aa%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/django-users/098cc4aa-374a-4e9a-9da1-ee49bab591aa%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/62492e1a-1aeb-48ef-b1c1-47d3bfc77124%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to