#11319: ForeignKey filters use the wrong field to prepare values for database
---------------------------------------------------+------------------------
          Reporter:  russellm                      |         Owner:  nobody
            Status:  new                           |     Milestone:  1.2   
         Component:  Database layer (models, ORM)  |       Version:  1.0   
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  0     
        Needs_docs:  0                             |   Needs_tests:  0     
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Comment (by akaihola):

 === Work-around ===

 Given

 {{{
 class Poll(models.Model):
     code = models.CharField(max_length=8)
     question = models.CharField(max_length=200)

 class Choice(models.Model):
     poll = models.ForeignKey(Poll, to_field='code')
     choice = models.CharField(max_length=200)
     votes = models.IntegerField()
 }}}

 instead of

 {{{
 Choice.objects.filter(poll=poll_instance)
 }}}

 you can use

 {{{
 Choice.objects.filter(poll__code=poll_instance.code)
 }}}

 The problem is limited to queries. Creating objects works correctly:

 {{{
 Choice(poll=poll_instance, choice='First choice', votes=100).save()
 Choice.objects.create(poll=poll_instance, choice='Other choice', votes=1)
 Poll.choice_set.create(choice='Bad choice', votes=10000000)
 }}}

 Beware that `get_or_create()` doesn't work, though!

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11319#comment:3>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to