I have (essentially) the following models:
class foo(models.model):
name = models.CharField(max_length=255)
size = models.PositiveIntegerField()
class bar(foo):
colour = models.CharField(max_length=25)
class baz(foo):
material = models.CharField(max_length=25)
What I want to do is filter these models based on url parameters. So, if开发者_StackOverflow中文版 the url is http://www.mysite.com/catalogue/foo?size=3
then all foo
,bar
, and baz
objects that are size 3 are displayed.
If the url is http://www.mysite.com/catalogue/foo?size=3&colour=red&colour=green
then all foo
object with the attributes size
and colour
(that is bar
objects) are displayed if the size is 3 and the colour is either red or green.
Can this work?
(Update: This Answer on a previously asked question is better than what I've written below.)
This article explains why this is inefficient in Django -- basically, because it would require expensive joins across the tables for all the subclasses. (I've written an ORM that does polymorphism like that, and the queries get out of hand very quickly. The Django guys made the right decision here.)
The article describes a method for writing a MixIn to query each subclass individually. If you need that behavior across your app, consider implementing something like that.
If this is for a single view, you may be better off (quicker, and easier to understand) just writing your view to query each subclass and create a list of all the results.
精彩评论