Opened 8 years ago

Closed 7 years ago

Last modified 5 years ago

#10243 closed (fixed)

to_field option problems in admin site

Reported by: tsawyer Owned by: mtredinnick
Component: Database layer (models, ORM) Version: master
Severity: Keywords: to_field
Cc: apollo13 Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


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

  • Component changed from Uncategorized to django.contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 8 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:3 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:4 Changed 7 years ago by mtredinnick

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

comment:5 Changed 7 years ago by mtredinnick

  • Component changed from django.contrib.admin to Database layer (models, ORM)
  • Owner changed from nobody to mtredinnick
  • Status changed from new to 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 Changed 7 years ago by apollo13

  • Cc apollo13 added

comment:7 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 7 years ago by mksoft

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 7 years ago by kmtracey

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 7 years ago by mksoft

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 7 years ago by russellm

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

comment:12 Changed 5 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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