I’m trying to make the following fit in the Django ORM. Having a Publish model which manages the publication of different kind of types of content (other models). This so I can easily do Publish.objects.all() and order them by date. I made a generic model as the following:
class Publish(models.Model):
""" Intermediary model for displaying and managing different types of content """
status = models.IntegerField(_('status'), choices=STATUS_CHOICES, default=1)
publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
The problem is that I want to attach this to different models. This has to be a OneToMany relationship. Because an article can only have one publication date. A generic relation is, as far as I know, a ManyToMany relationship.
I have tried restricting the max_num and extra in GenericTabularInline in admin.py, but this isn’t a great working solution. Does anyone know how to attach the Publish model to several different models, making it a required, one-to-many relationship? Many being the Publish model, one being for ex. an Article.
Your assumption is incorrect – Generic Foreign Keys are One to Many, not Many To Many. You can see that your model has
content_objectandobject_idfields – ie this Publish instance links to a single target object, although that object can have many Publish instances.What you actually want, it seems, is a OneToOne model. You can emulate that with the model you have, you can simply restrict the creation of Publish objects so that there can be only one for each target object. You can do this by setting
unique_togetheroncontent_typeandobject_idin the Meta subclass.Another option you might want to consider is instead of using generic relations, try multi-table model inheritance, where each object inherits from Publish.