Reputation: 1999
Lets say we have a class Place
with a class Restaurant
inheriting from it :
from django.db import models
class Place(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=80)
class Restaurant(Place):
serves_hot_dogs = models.BooleanField(default=False)
serves_pizza = models.BooleanField(default=False)
If I have a class Tag
related to places :
class Tag(models.Model):
name = models.CharField(max_length=50)
tagged = models.ManyToManyField(Place, related_name="tags")
For a given tag, how do I get a queryset selecting all Restaurants that have this tag, but not other kind of places ?
Upvotes: 2
Views: 255
Reputation: 6647
The easiest way to doing this is calling filter
from Restaurant.objects
with something like :
Restaurant.objects.filter(tags=tag)
But if you want call filter
from the Place.objects
, you must use one of Django polymorphism apps such as Django-Polymorphic in your parent model because Django not supports models polymorphism by default.
Note: Read This article about OOP Polymorphism & This article for some extra information about Django model inheritance.
Upvotes: 1