Django Admin: OneToOne Relation as an Inline?(Django Admin:OneToOne 关系作为内联?)
问题描述
我正在为一个 satchmo 应用程序整合管理员.Satchmo 使用 OneToOne 关系来扩展基本 Product
模型,我想在一页上对其进行编辑.
I am putting together the admin for a satchmo application. Satchmo uses OneToOne relations to extend the base Product
model, and I'd like to edit it all on one page.
可以将 OneToOne 关系作为内联吗?如果没有,将一些字段添加到我的管理员的给定页面最终将保存到 OneToOne 关系中的最佳方法是什么?
It is possible to have a OneToOne relation as an Inline? If not, what is the best way to add a few fields to a given page of my admin that will eventually be saved into the OneToOne relation?
例如:
class Product(models.Model):
name = models.CharField(max_length=100)
...
class MyProduct(models.Model):
product = models.OneToOne(Product)
...
我为我的管理员尝试了这个,但它不起作用,并且似乎需要一个外键:
I tried this for my admin but it does not work, and seems to expect a Foreign Key:
class ProductInline(admin.StackedInline):
model = Product
fields = ('name',)
class MyProductAdmin(admin.ModelAdmin):
inlines = (AlbumProductInline,)
admin.site.register(MyProduct, MyProductAdmin)
抛出此错误:<class 'satchmo.product.models.Product'>没有 <class 'my_app.models.MyProduct'>
这样做的唯一方法是 自定义表单?
Is the only way to do this a Custom Form?
刚刚尝试了以下代码直接添加字段...也不起作用:
edit: Just tried the following code to add the fields directly... also does not work:
class AlbumAdmin(admin.ModelAdmin):
fields = ('product__name',)
推荐答案
完全可以将内联用于 OneToOne 关系.但是,定义关系的实际字段必须位于内联模型上,而不是父模型上 - 与 ForeignKey 的方式相同.切换它,它会工作.
It's perfectly possible to use an inline for a OneToOne relationship. However, the actual field defining the relationship has to be on the inline model, not the parent one - in just the same way as for a ForeignKey. Switch it over and it will work.
评论后编辑:您说父模型已经向管理员注册:然后取消注册并重新注册.
Edit after comment: you say the parent model is already registered with the admin: then unregister it and re-register.
from original.satchmo.admin import ProductAdmin
class MyProductInline(admin.StackedInline):
model = MyProduct
class ExtendedProductAdmin(ProductAdmin):
inlines = ProductAdmin.inlines + (MyProductInline,)
admin.site.unregister(Product)
admin.site.register(Product, ExtendedProductAdmin)
2020 年更新(Django 3.1.1)
此方法仍然有效,但新 Django 版本中的某些类型已更改,因为现在应该将 ExtendedProductAdmin
中的 inlines
添加为列表而不是元组,像这样:
This method is still working but some types has changed in new Django version since inlines
in ExtendedProductAdmin
should now be added as list and not tuple, like this:
class ExtendedProductAdmin(ProductAdmin):
inlines = ProductAdmin.inlines + [MyProductInline]
否则你会得到这个错误:
Or you will get this error:
inlines = ProductAdmin.inlines + (MyProductInline,)
TypeError: can only concatenate list (not "tuple") to list
这篇关于Django Admin:OneToOne 关系作为内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django Admin:OneToOne 关系作为内联?
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01