Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 28, 2011 01:16:26

sushanth R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write joins ?


HI,

How to write joins in django,i have gone through below django documentation
,but joins are not working for my modelhttp://www.djangoproject.com/documentation/models/many_to_many/http://docs.djangoproject.com/en/1.2/topics/db/queries/#lookups-that-span-relationshipsand some blogs

My Model :


Class Profile(models.model)
name = models.CharField(max_length=50, primary_key=True)
assign = models.CharField(max_length=50)
doj = models.DateField()
dob = models.DateField()

class Meta:
db_table = u'profile'

class __str__(self):
return '%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)

class __unicode__(self):
return u'%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)


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


I need to query where i can join working and profile.

support

select working.*,profile.assign,profile.doj from working join profile where
name=w_name ;

I know django won't support joins,inner join is also okay for me.

Can any one help on this.........................?








--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 28, 2011 03:33:05

Sam L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write joins ?


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 28, 2011 05:07:43

sushanth R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write joins ?


hi Sam,

Here is example model:

class Profile(models.Model):
name = models.CharField(max_length=50, primary_key=True)
assign = models.CharField(max_length=50)
doj = models.DateField()
dob = models.DateField()

class Meta:
db_table = u'profile'

def __str__(self):
return '%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)

def __unicode__(self):
return u'%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)


class working(models.Model):
w_name =models.ForeignKey(Profile, db_column='w_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'

def __str__(self):
return '%s %s %s %s' % (
self.w_name,self.monday,self.tuesday,self.wednesday)

def __unicode__(self):
return u'%s %s %s %s' % (
self.w_name,self.monday,self.tuesday,self.wednesday)

>>> m=working.objects.filter(w_name__name='sushanth')
>>> m.query.as_sql()
(u'SELECT `working`.`id`, `working`.`w_name`, `working`.`monday`,
`working`.`tuesday`, `working`.`wednesday` FROM `working` WHERE
`working`.`w_name` = %s ', ('sushanth',))
>>>

But i am not getting any joins in above ...........

any suggestions .........



--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 28, 2011 05:13:53

Sam L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write joins ?


On 28 January 2011 16:07, sushanth Reddy <sushant...@gmail.com> wrote:
> hi Sam,
>
> Here is example model:
>
> class Profile(models.Model):
>      name = models.CharField(max_length=50, primary_key=True)
>      assign = models.CharField(max_length=50)
>      doj = models.DateField()
>      dob = models.DateField()
>
>      class Meta:
>         db_table = u'profile'
>
>      def __str__(self):
>          return  '%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)
>
>      def __unicode__(self):
>          return  u'%s %s %s %s' % ( self.name,self.assign,self.doj,self.dob)
>
>
> class working(models.Model):
>    w_name =models.ForeignKey(Profile, db_column='w_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'
>
>    def __str__(self):
>          return  '%s %s %s %s' % (
> self.w_name,self.monday,self.tuesday,self.wednesday)
>
>    def __unicode__(self):
>          return  u'%s %s %s %s' % (
> self.w_name,self.monday,self.tuesday,self.wednesday)
>
>>>> m=working.objects.filter(w_name__name='sushanth')
>>>> m.query.as_sql()
> (u'SELECT `working`.`id`, `working`.`w_name`, `working`.`monday`,
> `working`.`tuesday`, `working`.`wednesday` FROM `working` WHERE
> `working`.`w_name` = %s ', ('sushanth',))
>>>>
>
> But i am not getting any joins in above ...........

Because you're not asking for any information that requires a join to
retrieve. You asked for all 'working' objects where the foreign key
name is 'sushanth'. As the primary key of Profile is the name field,
that value is also stored in the 'working' model. Therefore you
haven't asked for any information that requires the join.

The join will be requested when you traverse the join in your model, e.g.

>>>> m=working.objects.filter(w_name__name='sushanth')
>>>> print m.w_name.assign

If you want to avoid the second database hit, use select_related(), e.g.

>>>> m=working.objects.filter(w_name__name='sushanth').select_related()

> any suggestions .........
>
>
>
> --
> 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.
>

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 28, 2011 05:47:16

sushanth R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to write joins ?


Thanks a ton sam...


--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 23rd of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support