views:

34

answers:

1

Hi, My models look like this:

Location:
    state
    county
    city
    street

Building:
    name
    location = ForeignKey(Location)

Now, in admin, when editing the Building, I want to have the ability to edit the Location in this way:how the admin should look like

So, it's like an inline, but with having Location in Building, not the oposite way.

A: 

Your problem is probably easier to solve if you keep a one-to-one relationship between building and location. For example, by subclassing building from location or by integrating the location fields into buildings.

I assume that not many buildings share the same location. So you would not save much using a foreign key to locations anyway. This foreign key also makes editing complicated. In particular, if you want separate entry fields for location components. Normally, you would first have to search existing locations for a match before creating a new location entry.

The following example makes Building a subclass of Location and groups building and location fields into two sections of the admin form. Your application will probably require some fine tuning though.

The model:

class Location(models.Model):
    state = models.CharField(max_length=30)
    county = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    street = models.CharField(max_length=30)

class Building(Location):
    name = models.CharField(max_length=120)

The admin form:

class BuildingAdmin(admin.ModelAdmin):
    fieldsets = (
        ('Building', {
            'fields': ('name',)
        }),
        ('Location', {
            'fields': (('state', 'county', 'city', 'street'),)
        }),
    )

admin.site.register(Building, BuildingAdmin)
Bernd Petersohn