#10243 closed (fixed)
to_field option problems in admin site
Reported by: | tsawyer | Owned by: | Malcolm Tredinnick |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | to_field | |
Cc: | Florian Apolloner | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The inline admin code seems to have a problem when using a to_field in the model definition. Given the following model:
class Users(models.Model): use_serial = models.DecimalField(decimal_places=0, max_digits=10, db_column='USE_SERIAL', primary_key=True) use_oracle_user = models.CharField(max_length=12, db_column='USE_ORACLE_USER', unique=True) def __unicode__(self): return self.use_oracle_user class UserAtSites(models.Model): uas_serial = models.DecimalField(decimal_places=0, max_digits=10, db_column='UAS_SERIAL', primary_key=True) uas_sit_serial = models.IntegerField(db_column='UAS_SIT_SERIAL') uas_use_oracle_user = models.ForeignKey(Users, to_field='use_oracle_user', db_column='UAS_USE_ORACLE_USER') def __unicode__(self): return '%s, %s' %(self.uas_sit_serial, self.uas_use_oracle_user)
and the following admin settings:
class UserAtSitesInline(admin.StackedInline): model = UserAtSites class UsersAdmin(admin.ModelAdmin): inlines = [UserAtSitesInline] class UserAtSiteAdmin(admin.ModelAdmin): pass admin.site.register(Users, UsersAdmin) admin.site.register(UserAtSites, UserAtSiteAdmin)
The inline admin is not finding the related objects by the foreign key uas_use_oracle_user. This appears to be because the query is selecting by the primary key (use_serial) rather than the declared to_field (use_oracle_user). The raw sql is logged as uas_use_oracle_user = 1 where we expect uas_use_oracle_user = 'tjs'.
I suspect that this is an inline admin problem, as doing the same sort of thing manually appears to work:
lNewUser = Users() lNewUser.use_serial = 1 lNewUser.use_oracle_user = 'tjs' lNewUser.save() lNewUserAtSites = UserAtSites() lNewUserAtSites.uas_serial = 10 lNewUserAtSites.uas_sit_serial = 123 lNewUserAtSites.uas_use_oracle_user = lNewUser lNewUserAtSites.save() lSelectedUser = UserAtSites.objects.all()[0] print lNewUserAtSites.uas_use_oracle_user_id # correctly prints tjs print lSelectedUser.uas_use_oracle_user.use_serial # correctly prints 1
We're also getting three fields in the inline admin section, one labelled uas serial, the next uas_sit_serial and the final one doesn't have a label.
Tested with svn revision 9826
Change History (12)
comment:1 by , 16 years ago
Component: | Uncategorized → django.contrib.admin |
---|
comment:2 by , 16 years ago
milestone: | post-1.0 |
---|
comment:3 by , 16 years ago
milestone: | → 1.1 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 16 years ago
Component: | django.contrib.admin → Database layer (models, ORM) |
---|---|
Owner: | changed from | to
Status: | new → assigned |
This is reporting two different bugs in one. Reducing the example to something a lot simpler (no db_columns
, etc) shows things a bit more easily. The extra row in the inline forms is #8903.
The other problem turns out to be a bug that's been in the related fields code for, literally, years. I'm working on it.
(Also, this bug has nothing to do with #9994 -- that ticket was much easier and not about the same thing, as it turned out.)
comment:6 by , 16 years ago
Cc: | added |
---|
comment:7 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:8 by , 15 years ago
Is it possible to backport this (and #10243) to 1.1.X branch ? It trips my code with Inline formset (not admin related).
comment:9 by , 15 years ago
comment:10 by , 15 years ago
I thought so, but it still persisted, after some more digging looks like dojango (dojo+django) duplicated the InlineForeignKeyField to use Dojo widgtes, but not updated with this change. Will submit a patch
Thank you kmtracey for the needed push :-)
Milestone post-1.0 deleted