Django GenericTabularInline 根本无法工作,但模型是正确的

发布于 2024-09-07 01:31:17 字数 1042 浏览 2 评论 0原文

我刚刚将我的一个模型转换为使用通用外键。该模型可以正常工作,数据库正在报告正确的值,并且我拥有的 API 可以与新的外键完美配合,零更改(保留相同的字段名称)。

然而,管理员完全失败了。内联没有显示任何数据,尽管有一些数据(见上文)。这是我的模型代码:

class CartItem(models.Model):
    cart = models.ForeignKey(Cart)
    quantity = models.PositiveIntegerField()
    price_item = models.DecimalField(max_digits=19, decimal_places=2)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_column="shop_item_id")
    shop_item = generic.GenericForeignKey("content_type", "object_id")

相当简单,对吧? admin.py 看起来像这样:

class CartItemInline(generic.GenericTabularInline):
   model = CartItem 

class CartAdmin(admin.ModelAdmin):
   inlines = [
        CartItemInline,
   ]

有趣的事实。如果我将“shop_item”添加到内联类中的字段元组中,则会收到此错误:

“CartItemInline.fields”引用表单中缺少的字段“shop_item”。

但是..它在那里?我已经尝试了所有方法,但 30 分钟后我就放弃了这个很可能非常简单的解决问题,但我今天的眼睛根本看不到。 :(

所有这些在使用 GenericForeignKey 之前都有效,所以不确定问题可能是什么。

感谢您的帮助。

I just converted one of my models to use a generic foreign key. The model is working correctly with these, database is reporting correct values, and the API I have is working perfectly with the new foreignkey with zero change (Kept the same field name).

However, the admin totally fails. The Inline shows no data, despite there being some (see above). Here is my model code:

class CartItem(models.Model):
    cart = models.ForeignKey(Cart)
    quantity = models.PositiveIntegerField()
    price_item = models.DecimalField(max_digits=19, decimal_places=2)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField(db_column="shop_item_id")
    shop_item = generic.GenericForeignKey("content_type", "object_id")

Fairly straightforward, right? admin.py looks like this:

class CartItemInline(generic.GenericTabularInline):
   model = CartItem 

class CartAdmin(admin.ModelAdmin):
   inlines = [
        CartItemInline,
   ]

Fun fact. If I add 'shop_item' to the fields tuple within the inline class I get this error:

'CartItemInline.fields' refers to field 'shop_item' that is missing from the form.

But .. it's there? I've tried everything and after 30 minutes I am just giving up on what is most likely a ridiculously simple to solve problem that my eyes simply cannot see today. :(

All this worked prior to using a GenericForeignKey so not sure what the issue could be anymore.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-09-14 01:31:17

嗯...也许

admin.site.register(CartItem, CartItemAdmin)

在 admin.py 的末尾?

http://docs .djangoproject.com/en/dev/ref/contrib/admin/#using-generic-relations-as-an-inline

我想这是因为你说“内联没有显示数据”所以我估计你没注册。我知道这听起来很愚蠢,但这就是我的想法。

Uhm...maybe

admin.site.register(CartItem, CartItemAdmin)

at the end of admin.py?

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#using-generic-relations-as-an-inline

I'm supposing this because you said "The Inline shows no data" so I'm guessing you didn't register it. I know it sounds stupid but that's what came on my mind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文