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[0].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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to