Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Inline form for OneToOne field in admin site

Tags:

python

django

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
  • 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.

like image 596
jax Avatar asked Jul 08 '14 03:07

jax


2 Answers

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.

like image 130
Anentropic Avatar answered Nov 10 '22 12:11

Anentropic


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:

admin interface

like image 25
Marco Frattallone Avatar answered Nov 10 '22 13:11

Marco Frattallone