Error: 'EventsRaw' has no ForeignKey to 'UserEvents' – Reverse-Related GenericForeignKey Inline Form in Django Admin

huangapple go评论48阅读模式
英文:

Error: 'EventsRaw' has no ForeignKey to 'UserEvents' - Reverse-Related GenericForeignKey Inline Form in Django Admin

问题

I can help you with the translation of the code and instructions you provided. Here's the translated code:

# models.py
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType

class UserEvents(models.Model):
    content_type = models.ForeignKey(
        ContentType, on_delete=models.CASCADE, null=True, blank=True
    )
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey("content_type", "object_id")

class EventsRaw(models.Model):
    raw_message = models.JSONField()

# For Creating test data
def create_events_raw():
    events_raw = EventsRaw()
    events_raw.raw_message = "test"
    events_raw.save()

    user_event = UserEvents()
    user_event.content_object = events_raw
    user_event.save()
# admin.py
from django.contrib import admin
from .models import UserEvents, EventsRaw

class EventsRawInline(admin.TabularInline):
    model = EventsRaw

@admin.register(UserEvents)
class User_EventsAdmin(admin.ModelAdmin):
    inlines = (EventsRawInline,)

If you have any further questions or need assistance with anything else, please let me know.

英文:

I'm encountering an error while attempting to add a reverse-related GenericForeignKey inline form to a model in Django admin. The specific error message states: "'EventsRaw' has no ForeignKey to 'UserEvents'."

Here's what I need assistance with:

  1. Displaying UserEvents in the Django admin dashboard.
  2. When navigating to the UserEvents page in the admin dashboard, I would like to see a list of all EventsRaw objects that are reverse-related.

Thank you in advance for your help!

# models.py
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class UserEvents(models.Model):
    content_type = models.ForeignKey(
        ContentType, on_delete=models.CASCADE, null=True, blank=True
    )
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey("content_type", "object_id")


class EventsRaw(models.Model):
    raw_message = models.JSONField()


# For Creating test data
def create_events_raw():
    events_raw = EventsRaw()
    events_raw.raw_message = "test"
    events_raw.save()

    user_event = UserEvents()
    user_event.content_object = events_raw
    user_event.save()
# admin.py
from django.contrib import admin
from .models import UserEvents, EventsRaw


class EventsRawInline(admin.TabularInline):
    model = EventsRaw


@admin.register(UserEvents)
class User_EventsAdmin(admin.ModelAdmin):
    inlines = (EventsRawInline,)

答案1

得分: 1

你可以在反向中添加一个GenericRelation,如下所示:

from django.contrib.contenttypes.fields import GenericRelation

class EventsRaw(models.Model):
    raw_message = models.JSONField()
    user_event = GenericRelation(UserEvent)

至于管理员部分,你可以使用GenericTabularInline替代TabularInline,如下所示:

from django.contrib.contenttypes.admin import GenericTabularInline

class EventsRawInline(GenericTabularInline):
    model = EventsRaw

然后,这将生成一个填充了内容类型的查询。但是,尽管GenericForeignKey是可能的,但它们通常会引入新的复杂性,因此通常是一个好主意。


> 注意:通常,Django 模型的名称是单数形式的,所以应该使用UserEvent而不是UserEvents

英文:

You can add a GenericRelation&nbsp;<sup>[Django-doc]</sup> in reverse, so:

<pre><code>from django.contrib.contenttypes.fields import <b>GenericRelation</b>

class EventsRaw(models.Model):
raw_message = models.JSONField()
user_event = <b>GenericRelation(UserEvents)</b></code></pre>

As for the admin, you can work with a GenericTabularInline&nbsp;<sup>[Django-doc]</sup> over a <s>TabularInline</s>:

<pre><code>from django.contrib.contenttypes.admin import <b>GenericTabularInline</b>

class EventsRawInline(<b>GenericTabularInline</b>):
model = EventsRaw</code></pre>

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 <s>UserEvents</s>.

huangapple
  • 本文由 发表于 2023年6月11日 22:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450954.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定