Opened 12 years ago

Closed 12 years ago

Last modified 10 years ago

#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


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, UsersAdmin), 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'

  lNewUserAtSites = UserAtSites()
  lNewUserAtSites.uas_serial = 10
  lNewUserAtSites.uas_sit_serial = 123
  lNewUserAtSites.uas_use_oracle_user = lNewUser

  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 Changed 12 years ago by Rob Hudson <treborhudson@…>

Component: Uncategorizeddjango.contrib.admin

comment:2 Changed 12 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:3 Changed 12 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:4 Changed 12 years ago by Malcolm Tredinnick

Essentially the same cause as #9994. I'll handle them both together.

comment:5 Changed 12 years ago by Malcolm Tredinnick

Component: django.contrib.adminDatabase layer (models, ORM)
Owner: changed from nobody to Malcolm Tredinnick
Status: newassigned

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 Changed 12 years ago by Florian Apolloner

Cc: Florian Apolloner added

comment:7 Changed 12 years ago by Russell Keith-Magee

Resolution: fixed
Status: assignedclosed

(In [10756]) Fixed #10243, #11043 -- Corrected handling of formsets over a ForeignKey that uses to_field, and by extension, fixed the admin for handling fields of that type. Thanks to apollo13 for the initial patch.

comment:8 Changed 11 years ago by Meir Kriheli

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 Changed 11 years ago by Karen Tracey

The 1.1.X branch was created a r11500, after r10756, so this fix was part of that branch when it was created. Whatever problem you are encountering is apparently not fixed by this fix, since you are already running with it if you are running 1.1.anything.

comment:10 Changed 11 years ago by Meir Kriheli

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 :-)

comment:11 Changed 11 years ago by Russell Keith-Magee

#11938 was another report, with some extra test cases.

comment:12 Changed 10 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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