A pretty good way to do this is using custom hand-written generic foreign key. You basically need fields "type" (which can be just a string) and the related object's id. And then implement everything needed by yourself.
Whatever you do you will have problems. There is no way to get all the different Tags in a single query (or well, there is always a way but that query is going to be ugly). So, if you have multiple different tag types for single object, you are going to need to run multiple queries to actually fetch the tags. I guess what I am saying here is that consider once again if you could change your requirements to something that fits SQL databases better. Having multiple m2m tables for the relations isn't a bad choice either. You will need to run multiple queries in any case, and it seems you will also need to handle the tags differently in your code. This will most likely perform much better than using generic relations based approach. Even if you can't touch the models, you can fake this by a model which has just two foreign keys. Anyways, using Django's generic foreign keys isn't always the best solution. It usually doesn't work well if you need to travel the foreign key in the wrong direction: if you need to get all the log entries for an object, then using foreign key works. If you need to fetch all objects having a given log entry (m2m reverse relation), it just doesn't work well. - Anssi On Feb 7, 9:11 pm, "arkai...@gmail.com" <arkai...@gmail.com> wrote: > Hi, > > So, I have the 4 entities represented below which are strong and > independent entities on my application, now the problem is that each > Article or Picture could be "tagged" with a Presenter or an Event, being as > they are the 4 of them independent entities that could become more complex > It doesn't look right to add Event and Presenter field to both Article and > Picture or the contrary, specially because they could be tagged with none. > In the long run as well other entities might need to be tagged and other > taggable entities might appear. > > > class Article(models.Model): > > > #Fields > > class Picture(models.Model): > > > #Fields > > class Presenter(models.Model): > > > # Fields > > class Event(models.Model): > > > # Fields > > > The closer I am getting is to some kind of double-headed Generic > > contenttype based intermediate model like this(haven't tested yet as it is > a bit more complex than that), but I am looking for ideas: > > > class GenericTag(models.Model): > > > # Event,Presenter instance.. > > tagcontent_type = models.ForeignKey(ContentType) > > > tagobject_id = models.PositiveIntegerField() > > tagcontent_object = generic.GenericForeignKey('tagcontent_type', > > 'tagobject_id') > > > # Picture,Article instance > > objcontent_type = models.ForeignKey(ContentType) > > > objobject_id = models.PositiveIntegerField() > > objcontent_object = generic.GenericForeignKey('objcontent_type', > > 'objobject_id') > > > And with that just do queries based on the information I have, I think > > there have to be more elegant ways to do this without stuffing all > tagmodels as fields into taggablemodels. > So, basically, there are several "taggable" objects and "tag" objects which > will never overlap and I would like a way of relating them with django > without touching the models themselves as they are entities of their own > apps. > > I was looking into a proxy model with a many2many field, but cannot add > fields to proxy models. > > Thanks > > -- > Arkaitz -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.