> class Container(models.Model): > # This is my container model. It will later be displayed as its > # title and a list of *Thing instances which all are formatted > # in a different fashion. > # ofc each Container can contain any number of any *Thing model > # instance > title = models.CharField(maxlength = 256) > # snip! > > class ImageThing(models.Model): > image = models.ImageField() #... > container = models.ForeignKey(Container) > sort_order= models.SmallIntegerField() > > class TextThing(models.Model): > text = models.TextField() > container = models.ForeignKey(Container) > sort_order= models.SmallIntegerField() > > # In my program this continues. I have about 10 *Thing models > > Now what i want is to get all *Thing instances that are related to my > Container instance and at the same time order all of them by their > sort_order field.
You can create one model that will serve as a basic child inside a container. Type specific models would then related to it as one-to-one (sorta inherited models): Container | +---- Thing ---- ImageThing | +---- Thing ---- TextThing In code this will look like this: class Container(model.Model): title = models.CharField(maxlength=256) class Thing(model.Models): # keeps container-related fields common to all things container = models.ForeignKey(Container) sort_order = models.SmallIntegerField() class ImageThing(model.Models): thing = models.OneToOneField(Thing) image = models.ImageField() class TextThing(model.Models): thing = models.OneToOneField(Thing) text = models.TextField() Then you can add a fancy methods to Thing that will return its typed counterpart (sorta "polymorphism"): class Thing(models.Model): ... def data(self): return getattr(self, 'image_thing', None) or \ getattr(self, 'text_thing', None) or \ getattr(self, 'another_thing') def __str__(self): return str(self.data()) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---