Opened 15 years ago

Closed 15 years ago

#9730 closed (duplicate)

When using model inheritance, raw_id_fields returns a model's name in the input field.

Reported by: grantmoney Owned by: Malcolm Tredinnick
Component: contrib.admin Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

# Parent Model

from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=99)
    description = models.CharField(max_length=400, blank=True)

# Child Models

class Country(Place):
    place = models.OneToOneField(Place, primary_key=True, parent_link=True, related_name='place_country')

class City(Place):
    place = models.OneToOneField(Place, primary_key=True, parent_link=True, related_name='place_city')
    country = models.ForeignKey(Country, related_name='city_country')

# Admin

from django.contrib import admin

class CityOptions(admin.ModelAdmin):
    raw_id_fields = ('country',)

admin.site.register(Place)
admin.site.register(Country)
admin.site.register(City, CityOptions)

# Description
First we start with a base model named Place, and then set up 2 Child models (Country and City in this case).

If we then use raw_id_fields for a ForeignKey to a model that inherits from the base model, instead of returning the model's pk/id, it instead returns the name of the object.

# First we create a Country
country, x = Country.objects.get_or_create(name="America")

If we now try to add a City using Django's admin, when selecting the country (which when using raw_id_fields is done via a pop up browser window), the input field will be given a value of "America" instead of the object's pk which in this case would be 1.

Going through where this might have started to occur, it seems to have started with the changes to ForeignKeyRawIdWidget in [8823].

Change History (3)

comment:1 by Malcolm Tredinnick, 15 years ago

Owner: changed from nobody to Malcolm Tredinnick
Status: newassigned

I'm fairly sure this is the same root cause as #9422 (although not a direct dupe). Basically, when we try to extract the related value for a model where that related value is a relation to another model, we fall on our face (e.g. a pk that is a relation to another field). We've got various workarounds in places at some points in the code, but it needs to be fixed holisitically.

comment:2 by Jacob Smullyan, 15 years ago

This is, I believe, a dupe of #9461, or vice versa.

comment:3 by Malcolm Tredinnick, 15 years ago

Resolution: duplicate
Status: assignedclosed

Yes, well spotted. I knew I'd seen this before somewhere.

Note: See TracTickets for help on using tickets.
Back to Top