I currently have a problem with generic keys. I do not know how to easily set two GenericRelation pointing to the same model as below:
Assume that we have the classes below :
class Pen(models.Model):
color = models.CharField(choices=COLORS)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
item = generic.GenericForeignKey('content_type', 'object_id')
class PencilCase(models.Model):
ballpoint_pens = generic.GenericRelation(Pen, related_name="ballpointpencil+")
fountain_pens = generic.GenericRelation(Pen, related_name="fountainpencil+")
The problem is that Django can not do the difference between a ballpoint pen and a fountain_pen. So as expected, each pen are both in the ballpoint_pens list and the fountain_pens one.
Does someone have an idea about it ?
In fact, it is very easy to do, I have just inherited my class Pen in two subclasses and changed the GenericRelation parameter, sorry for the disagreement :
class BallPointPen(Pen):
pass
class FountainPen(Pen):
pass
class PencilCase(models.Model)
ballpoint_pens = generic.GenericRelation(BallPointPen, related_name="ballpointpencil+")
fountain_pens = generic.GenericRelation(FountainPen, related_name="fountainpencil+")
And it works like a charm.