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

  • Home
  • SEARCH
  • 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 78917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:03:46+00:00 2026-05-10T21:03:46+00:00

Given: from django.db import models class Food(models.Model): Food, by name. name = models.CharField(max_length=25) class

  • 0

Given:

from django.db import models  class Food(models.Model):      '''Food, by name.'''      name = models.CharField(max_length=25)  class Cat(models.Model):      '''A cat eats one type of food'''      food = models.ForeignKey(Food)  class Cow(models.Model):      '''A cow eats one type of food'''      food = models.ForeignKey(Food)  class Human(models.Model):      '''A human may eat lots of types of food'''      food = models.ManyToManyField(Food) 

How can one, given only the class Food, get a set of all classes that it has ‘reverse relationships’ to. I.e. given the class Food, how can one get the classes Cat, Cow and Human.

I would think it’s possible because Food has the three ‘reverse relations’: Food.cat_set, Food.cow_set, and Food.human_set.

Help’s appreciated & thank you!

  • 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. 2026-05-10T21:03:47+00:00Added an answer on May 10, 2026 at 9:03 pm

    Either

    A) Use multiple table inheritance and create a ‘Eater’ base class, that Cat, Cow and Human inherit from.

    B) Use a Generic Relation, where Food could be linked to any other Model.

    Those are well-documented and officially supported features, you’d better stick to them to keep your own code clean, avoid workarounds and be sure it’ll be still supported in the future.

    — EDIT ( A.k.a. ‘how to be a reputation whore’ )

    So, here is a recipe for that particular case.

    Let’s assume you absolutely want separate models for Cat, Cow and Human. In a real-world application, you want to ask to yourself why a ‘category’ field wouldn’t do the job.

    It’s easier to get to the ‘real’ class through generic relations, so here is the implementation for B. We can’t have that ‘food’ field in Person, Cat or Cow, or we’ll run into the same problems. So we’ll create an intermediary ‘FoodConsumer’ model. We’ll have to write additional validation tests if we don’t want more than one food for an instance.

    from django.db import models from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic  class Food(models.Model):      '''Food, by name.'''      name = models.CharField(max_length=25)  # ConsumedFood has a foreign key to Food, and a 'eaten_by' generic relation class ConsumedFood(models.Model):     food = models.ForeignKey(Food, related_name='eaters')     content_type = models.ForeignKey(ContentType, null=True)     object_id = models.PositiveIntegerField(null=True)     eaten_by = generic.GenericForeignKey('content_type', 'object_id')  class Person(models.Model):     first_name = models.CharField(max_length=50)     last_name = models.CharField(max_length=50)     birth_date = models.DateField()     address = models.CharField(max_length=100)     city = models.CharField(max_length=50)     foods = generic.GenericRelation(ConsumedFood)  class Cat(models.Model):     name = models.CharField(max_length=50)     foods = generic.GenericRelation(ConsumedFood)      class Cow(models.Model):     farmer = models.ForeignKey(Person)     foods = generic.GenericRelation(ConsumedFood)     

    Now, to demonstrate it let’s just write this working doctest:

    ''' >>> from models import *  Create some food records  >>> weed = Food(name='weed') >>> weed.save()  >>> burger = Food(name='burger') >>> burger.save()  >>> pet_food = Food(name='Pet food') >>> pet_food.save()  John the farmer likes burgers  >>> john = Person(first_name='John', last_name='Farmer', birth_date='1960-10-12') >>> john.save() >>> john.foods.create(food=burger) <ConsumedFood: ConsumedFood object>  Wilma the cow eats weed  >>> wilma = Cow(farmer=john) >>> wilma.save() >>> wilma.foods.create(food=weed) <ConsumedFood: ConsumedFood object>  Felix the cat likes pet food  >>> felix = Cat(name='felix') >>> felix.save() >>> pet_food.eaters.create(eaten_by=felix) <ConsumedFood: ConsumedFood object>  What food john likes again ? >>> john.foods.all()[0].food.name u'burger'  Who's getting pet food ? >>> living_thing = pet_food.eaters.all()[0].eaten_by >>> isinstance(living_thing,Cow) False >>> isinstance(living_thing,Cat) True  John's farm is in fire ! He looses his cow. >>> wilma.delete()  John is a lot poorer right now >>> john.foods.clear() >>> john.foods.create(food=pet_food) <ConsumedFood: ConsumedFood object>  Who's eating pet food now ? >>> for consumed_food in pet_food.eaters.all(): ...    consumed_food.eaten_by <Cat: Cat object> <Person: Person object>  Get the second pet food eater >>> living_thing = pet_food.eaters.all()[1].eaten_by  Try to find if it's a person and reveal his name >>> if isinstance(living_thing,Person): living_thing.first_name u'John'  ''' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 263k
  • Answers 263k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If there are fatal errors, the script will stop no… May 13, 2026 at 12:07 pm
  • Editorial Team
    Editorial Team added an answer Use an ASP.NET custom validator. If for some reason you… May 13, 2026 at 12:07 pm
  • Editorial Team
    Editorial Team added an answer AutoMapper does not support this out of the box, as… May 13, 2026 at 12:07 pm

Related Questions

Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
In Django, given excerpts from an application animals likeso: A animals/models.py with: from django.db
Given a set of typical models: # Application A from django.db import models class
I've models for Books , Chapters and Pages . They are all written by

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.