tags:

views:

200

answers:

3

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.

A: 

I'd write a OneToOneGenericForeignKey, I don't know about any.

Or, if Article (or any other object) may be published only once, they could have a reference to Publish model.

However, I would not do that as it makes sense for me to have things published multiple times (categories, sites), so I'd just place restrictions via max_num/field/save/manager/whatever and leave my options open for the future.

Almad
+2  A: 

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.

Daniel Roseman
Thank you for your answer. My final solution was that I indeed added the validation at the model level with ``unique_together``.Than I changed the admin view for editing the models that had to be linked to ``Publish`` and added the ``Publish`` model form inside that view. Now it seems that ``Publish`` is part of the model.
wunki
A: 

It's not entirely clear in your question (others have seemed to conclude differently), but it appears to me that you're not asking for a OneToOne, you're asking for a OneToMany the opposite direction (where a Publish instance can have multiple Articles, but not the other way round). In that case you would abandon GenericForeignKey altogether, and simply ensure that all your content models have a normal ForeignKey to Publish. You could use abstract model inheritance to DRY that up a bit if you wanted.

Carl Meyer