Django Admin: OneToOne relationship as inline?

I am compiling admin for the satchmo application. Satchmo uses the OneToOne relationship to extend the base Product model, and I would like to edit it all on one page.

Is it possible to relate OneToOne as Inline? If not, how can I add several fields to a given page of my administrator, which will ultimately be saved with respect to OneToOne?

eg:

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

I tried this for my administrator, but it does not work and seems to be expecting a foreign key:

 class ProductInline(admin.StackedInline): model = Product fields = ('name',) class MyProductAdmin(admin.ModelAdmin): inlines = (AlbumProductInline,) admin.site.register(MyProduct, MyProductAdmin) 

What causes this error: <class 'satchmo.product.models.Product'> has no ForeignKey to <class 'my_app.models.MyProduct'>

Is this the only way to do this custom form ?

edit: Just tried the following code to add fields directly ... also doesn't work:

 class AlbumAdmin(admin.ModelAdmin): fields = ('product__name',) 
+48
python django django-admin inline one-to-one
Nov 16 '09 at 19:10
source share
4 answers

It is possible to use the built-in line for the OneToOne relationship. However, the actual field defining the relation should be on the inline model, and not on the parent — exactly the same as for ForeignKey. Switch it and it will work.

Edit after comment : you say that the parent model is already registered with the administrator: then unregister 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) 
+64
Nov 16 '09 at 19:49
source share

Using inheritance instead of OneToOne

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

Or use proxy classes

 class ProductProxy(Product) class Meta: proxy = True 

in admin.py

 class MyProductInlines(admin.StackedInline): model = MyProduct class MyProductAdmin(admin.ModelAdmin): inlines = [MyProductInlines] def queryset(self, request): qs = super(MyProductAdmin, self).queryset(request) qs = qs.exclude(relatedNameForYourProduct__isnone=True) return qs admin.site.register(ProductProxy, MyProductAdmin) 

In this option, your product will be embedded.

+5
Sep 28 '12 at 12:42 on
source share

Referring to the last question, what would be the best solution for several subtypes. For example, the Product class with a subtype of the Book class and a subtype of the CD class. As shown here, you will need to edit the product, as well as subtype elements for the book and subtype elements for the CD. Therefore, even if you want to add a book, you will also get CD margins. If you add a subtype, for example. DVD, you get three groups of subtype fields, while in reality you only need one group of subtypes, in the above example: books.

+4
Mar 14 '10 at 0:24
source share

Can you also try setting 'parent_link = True' to your OneToOneField?

https://docs.djangoproject.com/en/dev/topics/db/models/#specifying-the-parent-link-field

+1
Dec 14 '12 at 10:57
source share



All Articles