Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8225101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:18:04+00:00 2026-06-07T15:18:04+00:00

I’m currently doing something like this in my views.py: def home(request): ingredients = Ingredience.objects.all()

  • 0

I’m currently doing something like this in my views.py:

def home(request):
    ingredients = Ingredience.objects.all()
    drinks = Ingredience.objects.filter(category_id=1)
    fruits =  Ingredience.objects.filter(category_id=2)
    etc.
    meats = Ingredience.objects.filter(category_id=25)

    return render_to_response('home.html', {'ingredients': ingredients, 'drinks': drinks, 'fruits': fruits, 'meats': meats,}, context_instance=RequestContext(request))

And this is my models.py:

from django.db import models

class IngredienceCategory(models.Model):
    name = models.CharField(max_length=30)

    def __unicode__(self):
        return self.name


class Ingredience(models.Model):
    name = models.CharField(max_length=30)
    category = models.ForeignKey(IngredienceCategory, null=True, blank=True)

    def __unicode__(self):
        return self.name


class Food(models.Model):
    name = models.CharField(max_length=30)
    ingredients = models.ManyToManyField(Ingredience)

    def __unicode__(self):
        return self.name

Is there some trick in django to generate the template objects (i.e. drinks, fruits, meats) dynamically? I don’t even know where to start but I thought doing something like this: loop through all ingredients objects and return dictionary which would hold items grouped by category (as in the first example).

There are 2 problems I’m trying to solve with this:

  1. The code for each category object is almost the same so there is a
    lot of redundancy.
  2. I have to update views.py every time I add new
    category to the database.

EDIT: In my template I need to be able to access each object (i.e. drinks, fruits, meats etc.) by its own variable (e.g. {{ drinks }}.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:18:05+00:00Added an answer on June 7, 2026 at 3:18 pm

    Okay, based on your comments, you want to be able to access fruits, meats, etc in your template. Here is one possible solution that’s neither incredibly pythonic nor efficient, but that’s just to make it easier to understand:

    ingredients = Ingredience.objects.all()
    context = {}
    for i in ingredients:
        context[i.category.name + 's'] = context.get(i.category.name + 's', []) + [i]
    return render_to_response('home.html', context, context_instance=RequestContext(request))
    

    Then you could access it as fruits, meats, etc. Does Django have something for what you’re looking for exactly? Not as far as I know, it’s quite a rare use-case – but maybe some others could provide an answer to that. Essentially what you’re trying to achieve is some clever maneuvering of a context dictionary object so that the categories become the keys. If that’s the case, you might find some use also looking at this SO question.

    Update: Just to clarify what I’m doing in the code above: I create a dictionary called context, and then proceed to loop through all the ingredients. For every ingredient, I add a dictionary entry for the category name, containing an array with the Ingredient. If the name is already in the context dictionary, then it just appends this Ingredient. It does this until all the ingredients have been added to an array accessible by the name of the category it is in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.