[Answered ]-Error: 'EventsRaw' has no ForeignKey to 'UserEvents' โ€“ Reverse-Related GenericForeignKey Inline Form in Django Admin

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 GenericForeignKeys 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.

Leave a comment