views:

448

answers:

2

I cannot get the admin module to inline two same field models in one-to-one relations. To illustrate it, I've made the following example, a model Person uses two addresses:

class Client(models.Model):
    # Official address
    official_addr = models.OneToOneField(Address, related_name='official')
    # Temporary address
    temp_addr = models.OneToOneField(Address, related_name='temp')

I'd like to enable adding persons through Django admin interface with both addresses inlined. So far I have this code for admin configuration:

class ClientInline(admin.StackedInline):
    model = Client
    fk_name = "official_addr"

class ClientInline2(admin.StackedInline):
    model = Client
    fk_name = "temp_addr"

class AddressAdmin(admin.ModelAdmin):
    inlines = [ClientInline,ClientInline2]

admin.site.register(Address, AddressAdmin)

It works perfectly for the first address, but with both addresses the interface is acting crazy - duplicating Client's fields instead of addresses. What I am doing wrong? It there a better way to have two same models inlined?

A: 

I can't understand what you mean about 'acting crazy' by duplicating Client's fields. That's exactly what you've asked it to do - you have two inlines, both referring to Client. If this isn't what you want, you'll need to define it the other way round.

Daniel Roseman
I'd like to have two addresses inlined. Both of them will form one person. If the code I wrote is wrong, could you help me how to write "the other way round"? Thanks.
Viliam
A: 

Replace your admin with the following:

class ClientInline(admin.StackedInline):
    model = Client
    max_num = 1

class AddressAdmin(admin.ModelAdmin):
    inlines = [ClientInline]

admin.site.register(Address, AddressAdmin)
Thierry Lam