1

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 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?

Lexo
  • 470
  • 4
  • 20
  • What are you going to be using to generate these URLs in the first place? unless you intend to manually type in queries I don't see why the info can't be passed through a view on the backend – j_syk May 02 '11 at 20:10

1 Answers1

2

(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.

Community
  • 1
  • 1
Matt Miller
  • 3,501
  • 5
  • 27
  • 26
  • I realized I already asked a [similar question](http://stackoverflow.com/questions/3176731/django-inheritance-and-permalinks) before, with the same answer as your link. I'll see if my previous code will be reusable. – Lexo May 03 '11 at 07:44