On 28 January 2011 12:16, sushanth Reddy <sushant...@gmail.com> wrote:
> HI,
>
> How to write joins in django,i have gone through below django documentation
> ,but joins are not working for my model
>

<snip>

> Class working(models.model)
>   w_name =models.ForeignKey(Profile, db_column='name')
>   monday =  models.IntegerField(null=True, db_column='monday', blank=True)
>   tuesday =  models.IntegerField(null=True, db_column='tuesday', blank=True)
>   wednesday =  models.IntegerField(null=True, db_column='wednesday',
> blank=True)
>
>   class Meta:
>         db_table = u'working'
>
>   lass __str__(self):
>          return  '%s %s %s %s' % (
> self.w_name,self.monday,self.tuesday,self.wednesday)
>
>   class __unicode__(self):
>          return  u'%s %s %s %s' % (
> self.w_name,self.monday,self.tuesday,self.wednesday)
>
>
> I am trying to do join between two tables profile and workingday
>
>      like m=working.objects.filter(name='sushanth').select_related()
>
>
> if i run above query i'll get
>
> Traceback (most recent call last):
>   File "<console>", line 1, in <module>
>   File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 129,
> in filter
>     return self.get_query_set().filter(*args, **kwargs)
>   File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 498,
> in filter
>     return self._filter_or_exclude(False, *args, **kwargs)
>   File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 516,
> in _filter_or_exclude
>     clone.query.add_q(Q(*args, **kwargs))
>   File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line
> 1675, in add_q
>     can_reuse=used_aliases)
>   File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line
> 1569, in add_filter
>     negate=negate, process_extras=process_extras)
>   File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line
> 1737, in setup_joins
>     "Choices are: %s" % (name, ", ".join(names)))
> FieldError: Cannot resolve keyword 'name' into field. Choices are:  monday,
> tuesday, wednesday,  w_name

The error message is quite descriptive already. To perform the filter
on the foreign key field, you use the name of the foreign key, *not*
the name of the field the foreign key refers to. So this will work -

m=working.objects.filter(w_name='sushanth').select_related()

BTW, setting the name field in your Profile model as the primary key
is probably going to be a bad idea - what happens when two people have
the same name? An ID is probably better, and if you leave the
primary_key argument out, Django will automatically create one for
you.

Also, in Python, classes should be named in TitleCase by convention.

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

Reply via email to