简体   繁体   English

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

[英]Django - Inline form for OneToOne field in admin site

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. Django 反向管理。 Unfortunately did not work with Django 1.6 and I am not saavy enough to make it work with 1.6不幸的是没有与 Django 1.6 一起工作,我不够聪明,无法让它与 1.6 一起工作
  • Several suggection in stackover flow on using proxy models and abstract base class and those did not work either. stackover 流程​​中关于使用代理模型和抽象基类的一些建议也没有奏效。

I would really appreciate if someone could help me to find a workaround for it.如果有人能帮我找到解决方法,我将不胜感激。

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 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: ...您需要from django import forms顶部的from django import forms ,然后执行以下操作:

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' ie if you don't need a custom form for the inline model.请注意,它向您展示了指定内联的三种不同方式...第一种只是通过字段名称'business_addr'即如果您不需要内联模型的自定义表单。

I have Installed:我已经安装:

  • Django==1.6.5姜戈==1.6.5
  • MySQL-python==1.2.4 MySQL-python==1.2.4
  • South==0.8.1南==0.8.1

and the code below works form me:下面的代码适用于我:

models.py模型.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:这是管理界面:

管理界面

For those who looking solution for Django 2.0: you can just use this library: https://pypi.org/project/django-reverse-admin/对于那些寻找 Django 2.0 解决方案的人:你可以使用这个库: https : //pypi.org/project/django-reverse-admin/

It worked well for me.它对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM