Django - Inline form for OneToOne field in admin site

后端 未结 3 843
天命终不由人
天命终不由人 2021-01-12 13:24

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(mo         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-12 13:28

    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:

    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:

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

    There's another problem in the example code here line #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.

提交回复
热议问题