Django: Generic one-to-many relationship with Contenttypes -
I am trying to make the following fit in Django ORM. A model with a publish
which manages the publication of different types of content (other models). That's why I can easily Publish.objects.all ()
and order them according to date. I created a normal model as:
publish class (models.Model): "" Intermediate model to display and manage various types of content "" "status = model .interfold (_ ('position'), option = STATUS_CHOICES, default = 1) Publish = Model DataTifield (_ ('Publish'), default = datetime.datetime.now) content_type = model.Forinzee (content) object_id = Models.PositiveIntegerField () Content_object = generic.GenericForeignKey ('content_type', 'object_id')
The problem is that I want to attach it to different models, it should be a OneToMany
relationship, because an article can only have a publication date. There is a general relationship, as far as I know, a ManyToMany
relationship.
I have tried to limit the max_num
and extra generic tabular inline
, but this is not a great work solution. Does anyone know Publishing model to add to the number of different models, it is a necessary one-to-many relationships? Many people are published models, which is a paragraph for the former.
Your assumption is incorrect - Generic foreign keys are many from one, many can not see many That your model has fields in content_object
and object_id
- that is to publish examples of a single target object, although there may be several published examples in that object.
What you really want, it seems, is a one-tone model. You can simulate with the model that you have, you only prohibit the creation of objects to be published. So that only one can be for each object object. You can do this in unique_together
at content_type
and object_id
in the meta sub-category.
class (Models.Model): ... fields ... class meta: unique_together ('content_type', 'object_id')
Another option you might want to consider, instead of using normal relationships, try multi-table model succession, where each object gets published.
Comments
Post a Comment