Django - 管理站点中OneToOne字段的内联表单

时间:2022-05-16 07:22:45

Hi This question has been asked many times but unfortunately I could not find an answer for it that would actually work. Below are my models:

嗨这个问题已被多次询问,但遗憾的是我无法找到真正有用的答案。以下是我的模特:

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

class Address(models.Model):
    person = models.OneToOneField(Person)
    ...

Then in the admin I have:

然后在管理员我有:

class AddressInline(admin.StackedInline):
    model = Address


class PersonAdmin(admin.ModelAdmin):
    inlines = (AddressInline)

admin.site.register(Person, PersonAdmin)

and then i get this infamous error:

然后我得到这个臭名昭着的错误:

<class 'address.models.Address'> has no ForeignKey to <class 'person.models.Person'>

I have tried:

我努力了:

  • django-reverse-admin. Unfortunately did not work with Django 1.6 and I am not saavy enough to make it work with 1.6
  • Django的反向联系。不幸的是没有使用Django 1.6而且我没有足够的saavy来使它与1.6一起使用

  • Several suggection in stackover flow on using proxy models and abstract base class and those did not work either.
  • 堆栈中的几个建议流程使用代理模型和抽象基类,这些也没有用。

I would really appreciate if someone could help me to find a workaround for it.

如果有人能帮我找到解决方法,我真的很感激。

2 个解决方案

#1


10  

I have not tried it, but this gist appears to be based on the code in django-reverse-admin but updated to work on Django 1.6:

我没有尝试过,但这个要点似乎是基于django-reverse-admin中的代码,但更新后才能使用Django 1.6:

https://gist.github.com/mzbyszewska/8b6afc312b024832aa85

Note that this part of the example code is wrong:

请注意,示例代码的这一部分是错误的:

class AddressForm(models.Form):
    pass

...you need to from django import forms at the top and then do something like:

...你需要从顶部的django导入表单然后执行以下操作:

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address

There's another problem in the example code here line #46:

第46行的示例代码中还有另一个问题:

inline_reverse = ('business_addr', ('home_addr', AddressForm), ('other_addr' (
    'form': OtherForm
    'exclude': ()
)))

should probably be:

应该是:

inline_reverse = ('business_addr', ('home_addr', AddressForm), ('other_addr', {
    'form': OtherForm,
    'exclude': ()
}))

note that it shows you the three different ways to specify an inline... the first is just by field name 'business_addr' i.e. if you don't need a custom form for the inline model.

请注意,它显示了指定内联的三种不同方式...第一种方法只是按字段名称'business_addr',即如果您不需要内联模型的自定义表单。

#2


3  

I have Installed:

我已安装:

  • Django==1.6.5
  • MySQL-python==1.2.4
  • South==0.8.1

and the code below works form me:

以下代码构成了我:

models.py

# -*- coding: utf-8 -*-

from django.db import models

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


class Address(models.Model):
    person = models.OneToOneField(Person)
    street = models.CharField(max_length=100)

admin.py

# -*- coding: utf-8 -*-
from django.contrib import admin

from .models import *


class AddressInline(admin.StackedInline):
    model = Address


class PersonAdmin(admin.ModelAdmin):
    inlines = (AddressInline,)

admin.site.register(Person, PersonAdmin)
admin.site.register(Address)

And this is the admin inferface:

这是管理员的界面:

Django  - 管理站点中OneToOne字段的内联表单

#1


10  

I have not tried it, but this gist appears to be based on the code in django-reverse-admin but updated to work on Django 1.6:

我没有尝试过,但这个要点似乎是基于django-reverse-admin中的代码,但更新后才能使用Django 1.6:

https://gist.github.com/mzbyszewska/8b6afc312b024832aa85

Note that this part of the example code is wrong:

请注意,示例代码的这一部分是错误的:

class AddressForm(models.Form):
    pass

...you need to from django import forms at the top and then do something like:

...你需要从顶部的django导入表单然后执行以下操作:

class AddressForm(forms.ModelForm):
    class Meta:
        model = Address

There's another problem in the example code here line #46:

第46行的示例代码中还有另一个问题:

inline_reverse = ('business_addr', ('home_addr', AddressForm), ('other_addr' (
    'form': OtherForm
    'exclude': ()
)))

should probably be:

应该是:

inline_reverse = ('business_addr', ('home_addr', AddressForm), ('other_addr', {
    'form': OtherForm,
    'exclude': ()
}))

note that it shows you the three different ways to specify an inline... the first is just by field name 'business_addr' i.e. if you don't need a custom form for the inline model.

请注意,它显示了指定内联的三种不同方式...第一种方法只是按字段名称'business_addr',即如果您不需要内联模型的自定义表单。

#2


3  

I have Installed:

我已安装:

  • Django==1.6.5
  • MySQL-python==1.2.4
  • South==0.8.1

and the code below works form me:

以下代码构成了我:

models.py

# -*- coding: utf-8 -*-

from django.db import models

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


class Address(models.Model):
    person = models.OneToOneField(Person)
    street = models.CharField(max_length=100)

admin.py

# -*- coding: utf-8 -*-
from django.contrib import admin

from .models import *


class AddressInline(admin.StackedInline):
    model = Address


class PersonAdmin(admin.ModelAdmin):
    inlines = (AddressInline,)

admin.site.register(Person, PersonAdmin)
admin.site.register(Address)

And this is the admin inferface:

这是管理员的界面:

Django  - 管理站点中OneToOne字段的内联表单