Opened 8 years ago

Closed 8 years ago

#4957 closed (duplicate)

Query for ForeignKey using implicit exact and to_field breaks

Reported by: jdunck Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: qs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Given these models:

class District(models.Model, models.GeoMixin):
    tea_number = models.CharField(maxlength=20, unique=True)

class School(models.Model, models.GeoMixin):
    district = models.ForeignKey(District, to_field='tea_number')

I expect this to work:

a_district = District.objects.all()[0]
Schools.objects.filter(district=a_district)

So that a_district's tea_nuumber attribute is used for the criteria against the School table rather than the District's PK.

django.db.models.query.lookup_inner (line 1082) has the bug:

                # RelatedObject is from a 1-1 relation,
                # No need to join; get the pk value from the related object,
                # and compare using that.
                column = current_opts.pk.name

I'll try to work up a patch for it. Tired now.

Change History (2)

comment:1 Changed 8 years ago by mtredinnick

  • Component changed from Core framework to Database wrapper
  • Keywords qs-rf-fixed added
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 8 years ago by ubernostrum

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #4088.

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