1๐
โ
You can add a GenericRelation
ย [Django-doc] in reverse, so:
from django.contrib.contenttypes.fields import GenericRelation
class EventsRaw(models.Model):
raw_message = models.JSONField()
user_event = GenericRelation(UserEvents)
As for the admin, you can work with a GenericTabularInline
ย [Django-doc] over a :TabularInline
from django.contrib.contenttypes.admin import GenericTabularInline
class EventsRawInline(GenericTabularInline):
model = EventsRaw
This will then produce a query with the content type filled in. But while GenericForeignKey
s are indeed possible, they often will introduce a new level of complexity, and therefore are often not a good idea.
Note: normally a Django model is given a singular name, so
UserEvent
instead of.UserEvents
Source:stackexchange.com