Django管理员:OneToOne关系作为内联? [英] Django Admin: OneToOne Relation as an Inline?

查看:200
本文介绍了Django管理员:OneToOne关系作为内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在把一个satchmo应用程序的管理员放在一起。 Satchmo使用OneToOne关系来扩展基本的产品模型,我想在一个页面上进行编辑。



可以将OneToOne关系作为Inline吗?如果没有,添加几个字段到管理员的给定页面中最终将被保存到OneToOne关系中的最佳方法是什么?



例如: p>

  class Product(models.Model):
name = models.CharField(max_length = 100)
..

class MyProduct(models.Model):
product = models.OneToOne(Product)
...

我为我的管理员尝试过,但它不起作用,似乎期待一个外键:

  class ProductInline(admin.StackedInline):
model = Product
fields =('name',)

class MyProductAdmin(admin.ModelAdmin )
inlines =(AlbumProductInline,)

admin.site.register(MyProduct,MyProductAdmin)

哪个会抛出这个错误:< class'satchmo.product.models.Product'>没有ForeignKey到< class'my_app.models.MyProduct'>



是唯一的方法来做这个自定义表单



编辑:刚刚尝试以下代码直接添加字段...也不起作用:

  class AlbumAdmin(admin.ModelAdmin):
fields =('product__name',)


解决方案

完全可以使用一个OneToOne关系的内联。然而,定义关系的实际字段必须在内联模型上,而不是父级模型 - 就像外键一样。



评论后编辑:您说父母模型已经注册到管理员:然后取消注册,重新注册。

  from original.satchmo.admin import ProductAdmin 

class MyProductInline(admin.StackedInline )
model = MyProduct

class ExtendedProductAdmin(ProductAdmin):
inlines = ProductAdmin.inlines +(MyProductInline,)

admin.site.unregister (产品)
admin.site.register(Product,ExtendedProductAdmin)


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.

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?

for example:

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)

Which throws this error: <class 'satchmo.product.models.Product'> has no ForeignKey to <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',)

解决方案

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)

这篇关于Django管理员:OneToOne关系作为内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆