Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Generic one-to-many relationship with Contenttypes

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.

like image 415
wunki Avatar asked Feb 28 '23 23:02

wunki


1 Answers

Your assumption is incorrect - Generic Foreign Keys are One to Many, not Many To Many. You can see that your model has content_object and object_id fields - 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_together on content_type and object_id in the Meta subclass.

class Publish(models.Model):
    ... fields ...

    class Meta:
        unique_together('content_type', 'object_id')

Another option you might want to consider is instead of using generic relations, try multi-table model inheritance, where each object inherits from Publish.

like image 152
Daniel Roseman Avatar answered May 11 '23 10:05

Daniel Roseman