When using model inheritance, raw_id_fields returns a model's name in the input field.
|Reported by:||grantmoney||Owned by:||mtredinnick|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
# 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')
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)
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 .
Change History (3)
comment:1 Changed 8 years ago by mtredinnick
- Needs documentation unset
- Needs tests unset
- Owner changed from nobody to mtredinnick
- Patch needs improvement unset
- Status changed from new to assigned