Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django model foreign key queryset selecting related fields

I am trying to select from the join of the two related tables in DJango as shown below. But I am not able get the field name of the other table.

In SQL we can write:

select person.phonenumber,membership.* from membership where person=name ;

This displays all the membership fields along with phonenumber, but in Django it throws error:

Cannot resolve keyword 'phonenumber' into field

I have even tried with select_related but no use. Am I doing anything wrong?

model.py:

from django.db import models

# Create your models here.
class Person(models.Model):
    name = models.CharField(primary_key=True,max_length=128)
    phonenumber = models.CharField(max_length=128)
    city = models.CharField(max_length=128)


    def __unicode__(self):
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person)
    date_joined = models.DateField()
    invite_reason = models.CharField(max_length=64)


>>> print m.query
SELECT `myapp_membership`.`id`, `myapp_membership`.`person_id`, `myapp_membership`.`date_joined`, `myapp_membership`.`invite_reason` FROM `myapp_membership` WHERE `myapp_membership`.`person_id` = x 
>>> m=Membership.objects.filter(person__name='x').select_related()
>>> print m.query
SELECT `myapp_membership`.`id`, `myapp_membership`.`person_id`, `myapp_membership`.`date_joined`, `myapp_membership`.`invite_reason`, `myapp_person`.`name`, `myapp_person`.`phonenumber`, `myapp_person`.`city` FROM `myapp_membership` INNER JOIN `myapp_person` ON (`myapp_membership`.`person_id` = `myapp_person`.`name`) WHERE `myapp_membership`.`person_id` = x 
>>> m=Membership.objects.filter(person__name='x').values('person','phonenumber').select_related()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/query.py", line 510, in values
    return self._clone(klass=ValuesQuerySet, setup=True, _fields=fields)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/query.py", line 766, in _clone
    c._setup_query()
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/query.py", line 889, in _setup_query
    self.query.add_fields(self.field_names, False)
  File "/usr/local/lib/python2.6/dist-packages/Django-1.2.4-py2.6.egg/django/db/models/sql/query.py", line 1537, in add_fields
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'phonenumber' into field. Choices are: date_joined, id, invite_reason, person
like image 657
sush Avatar asked Mar 21 '11 16:03

sush


People also ask

What is the difference between Select_related and Prefetch_related?

select_related() “follows” foreign-key relationships, selecting additional related-object data when it executes its query. prefetch_related() does a separate lookup for each relationship and does the “joining” in Python.


2 Answers

phonenumber is a field on person, so you need to pass in person__phonenumber

m=Membership.objects.filter(person__name='x').values('person','person__phonenumber').
like image 72
Yuji 'Tomita' Tomita Avatar answered Oct 14 '22 03:10

Yuji 'Tomita' Tomita


Also if you want a queryset (so not to use values) you can do this:

m=Membership.objects.filter(person__name="x").select_related('person', depth=1)

Sadly I do not know how to only get one field from that table and still get a queryset.

like image 40
Virgil Balibanu Avatar answered Oct 14 '22 03:10

Virgil Balibanu