英文:
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:
- Displaying UserEvents in the Django admin dashboard.
- 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
<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
<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 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 <s>UserEvents
</s>.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论