On 20 November 2010 02:51, Micah Carrick <mi...@greentackle.com> wrote: > I'm having trouble coming up with a pretty solution to a seemingly simple > task. I'm relatively new to Django. > > I want to allow the end user to control various lists of links on the site > used for navigation. The admin should allow the creation of "link groups" > which have a collection of "links". These links would reference a > pre-determined list of models. Let's say, for example, that there is a "link > group" created for the footer links of a website. In the admin section, a > user could add a link to a specific blog (from the Blog model), another link > to the about us page (flatpages), etc. > > In other words, I'm trying to associate individual records from a number of > tables together as a group of objects having a URL. I'm trying to find a > nice, abstract solution. Obviously I don't want actual URLs in the database. > This is something I would use frequently so I want to see if I can find or > write an app to do this--if I can come up with an elegant solution. > > This would be nice, but, I can't imagine how it could be possible: > > > class LinkGroup(models.Model): > site = models.ForeignKey(Site) > name = models.CharField() > > class Links(models.Model): > link_group = ForeignKey(LinkGroup) > model_type = ??? > model_id = ForeignKey(<to the PK of the above the model_type>) # no can > do! > sort_order = PositiveIntegerField(default=100) > > > This is an idea, however, I don't like having to reference the import in the > DB. It's just begging for problems. > > > class LinkModel(models.Model): > name = models.CharField() # such as "Flat Page" > model = models.CharField() # such as "myapp.models.FlatPage" > > class LinkGroup(models.Model): > site = models.ForeignKey(Site) > name = models.CharField() # such as "Navigation Links" > > class Link(models.Model): > text = CharField() # such as "About Us" > link_group = ForeignKey(LinkGroup) > model = ForeignKey(LinkModel) > model_id = PositiveIntegerField() # such as the PK for the > myapp.models.FlatPage model > sort_order = PositiveIntegerField(default=100) > > > Any suggestions?
You should checkout generic foreign keys[1]. It's a standard way of building models that need to reference rows in more then one table. So your models would be something like this: from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes import generic class LinkGroup(models.Model): site = models.ForeignKey(Site) name = models.CharField() # such as "Navigation Links" class Link(models.Model): link_group = ForeignKey(LinkGroup) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() linked_object = generic.GenericForeignKey('content_type', 'object_id') [1]: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations -- Łukasz Rekucki -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.