Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric many2many relationships in Django
    text
    copied!<p>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.<br> In the long run as well other entities might need to be tagged and other taggable entities might appear.</p> <pre><code>class Article(models.Model): #Fields class Picture(models.Model): #Fields class Presenter(models.Model): # Fields class Event(models.Model): # Fields </code></pre> <p>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:</p> <pre><code>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') </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload