OneToOneField relation and bidirectional StackedInline behavior in contrib.admin context

1,704 views
Skip to first unread message

klein.s...@gmail.com

unread,
Jan 9, 2009, 8:33:29 AM1/9/09
to Django users
Hi,

First, I begin with one example :

I've this in test1/models.py

from django.db import models

class ClassA(models.Model):
foo = models.CharField("Foo", max_length = 100)

class ClassB(models.Model):
bar = models.CharField("bar", max_length = 100)

rel_a = models.OneToOneField("ClassA")


I've this in test1/admin.py :

from django.contrib import admin
from django_test1.test1.models import ClassA, ClassB

class ClassAInline(admin.StackedInline):
model = ClassA

class ClassBAdmin(admin.ModelAdmin):
inlines = [
ClassAInline
]

class ClassBInline(admin.StackedInline):
model = ClassB

class ClassAAdmin(admin.ModelAdmin):
inlines = [
ClassBInline
]

admin.site.register(ClassA, ClassAAdmin)
admin.site.register(ClassB, ClassBAdmin)

Now, if I'm going to :

* http://127.0.0.1:8000/admin/test1/classa/add/ => it's work with
success

next, if I'm going to :

* http://127.0.0.1:8000/admin/test1/classb/add/ => I've Exception
error :

<class 'test1.models.ClassA'> has no ForeignKey to <class
'test1.models.ClassB'>

/home/harobed/projets/virtualenv_django/lib/python2.5/site-
packages/Django-1.0.2_final-py2.5.egg/django/forms/models.py in
_get_foreign_key, line 525

This error is understandable because actually ClassA haven't
ForeignKey but the relation
between ClassA and ClassB is OneToOneField then ClassA know how to
access to his classb object.

This behavior is one missing admin model feature or my aims is
inconsistent ?

Thanks for your help,
Stephane

Malcolm Tredinnick

unread,
Jan 9, 2009, 8:13:56 PM1/9/09
to django...@googlegroups.com
On Fri, 2009-01-09 at 05:33 -0800, klein.s...@gmail.com wrote:
[...]

> This error is understandable because actually ClassA haven't
> ForeignKey but the relation
> between ClassA and ClassB is OneToOneField then ClassA know how to
> access to his classb object.
>
> This behavior is one missing admin model feature or my aims is
> inconsistent ?

It's just a case of things not intended to work that way. The inline
editing feature works consistently in one direction for related models
(foreign key and one-to-one are essentially the same for purposes here),
not bidirectionally.

Maybe you could knock up a patch to make this work and maybe it's
worthwhile (I don't really have a strong opinion either way, beyond
thinking that there's really no end to people twisting the admin
interface beyond intended usage and perhaps the should re-evaluate their
use-cases). This is "scratch your own itch" territory, I suspect.

Regards,
Malcolm


peschler

unread,
Jan 28, 2009, 9:06:54 PM1/28/09
to Django users
Hi,

actually I am facing the same problem here. I did quite a search on
the subject and I found no real solution to this use case. Here are my
models:

---
contacts/models.py:
class Address(models.Model):
street = models.CharField(max_length=255)
city = models.CharField(max_length=255)
region = models.CharField(max_length=255)
...

contacts/admin.py:
class AddressInline(admin.StackedInline):
model = Address
extra = 0

location/models.py:
class Location(models.Model):
address = models.ForeignKey(Address)

location/admin.py:
class LocationAdmin(admin.ModelAdmin):
inlines = [AddressInline,]

other/models.py:
class Other(models.Model):
address = models.ForeignKey(Address)

other/admin.py
class OtherAdmin(admin.ModelAdmin):
inlines = [AddressInline,]
---

The point is that the address is used in several other models and will
be used in even more models in the future. And it should definitely
not know about those other models.

Now in order to display the address admin fields inline on the
"Location" and "Other" model I need to overload some functions of the
LocationAdmin or OtherAdmin class. This is not an easy task and it
could be speed up by making the reverse inline admin work.

I think this is not an esoteric use case, so this might not be
"twisting the admin interface beyond intended usage". But as I am not
that much experienced with database design I wonder if there is some
other way to code the above models in order to get the address into
the admin pages of the other model (by using existing Django admin
features).

Regards,
Peter Eschler

On 10 Jan., 02:13, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
Reply all
Reply to author
Forward
0 new messages