Re: reverse foreign key lookup in extra()

2008-12-22 Thread Fereshteh

Thanks a lot Malcolm for your explanatory information. I think I get
the concept now :)

On Dec 20, 9:36 pm, Malcolm Tredinnick 
wrote:
> On Fri, 2008-12-19 at 11:10 -0800, Fereshteh wrote:
> > Hi,
> > I am trying to add a filtering functionality to my web application. I
> > create a WHERE clouse text in the client side as text and post it to
> > the server.
>
> > My Django modele lookes like this:
> > class Projects(models.Model):
> >     id = models.AutoField(primary_key=True)
> >     name = models.TextField()
> >     class Meta:
> >         db_table = 'projects'
>
> > class Timesheet(models.Model):
> >     ts_id = models.AutoField(primary_key=True)
> >     user = models.ForeignKey(Users, db_column='ts_user',
> > related_name='ts_user_child')
> >     project = models.ForeignKey(Projects, db_column='project',
> > related_name='project_child', null='true')
> >     class Meta:
> >         db_table = 'timesheet'
>
> > I use the following in my Django view to retrieve the data according
> > to the filtering argument received:
> > filter_str = request.POST['filter_str']
> > #filter_str contains something like this:  "project__name = 'Django
> > supported web app'"
> > Timesheet.objects.filter(user = id).extra(where = [filter_str])
>
> > But it gives me the error:
>
> > column project__name does not exist
>
> > Without using extra the same query works fine so the column does
> > exist.
>
> Your conclusion here is incorrect. There is no column in the database
> table called "project__name". When you pass that string to a filter()
> call, Django does a lot of processing and works out the right database
> table and column name to use for the query. Have a look at the output of
>
>         Timesheet.objects.filter(project__name="foo").query.as_sql()
>
> for example. You will not see any reference to a column called
> "project__name" there. If you are using extra(), you need to use the
> correct column names: extra() only exists as a way to write almost raw
> SQL portions.
>
> It sounds a lot like you really want to be using a second filter() call,
> not an extra() call.
>
> Regards,
> Malcolm
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: reverse foreign key lookup in extra()

2008-12-20 Thread Malcolm Tredinnick

On Fri, 2008-12-19 at 11:10 -0800, Fereshteh wrote:
> Hi,
> I am trying to add a filtering functionality to my web application. I
> create a WHERE clouse text in the client side as text and post it to
> the server.
> 
> My Django modele lookes like this:
> class Projects(models.Model):
> id = models.AutoField(primary_key=True)
> name = models.TextField()
> class Meta:
> db_table = 'projects'
> 
> class Timesheet(models.Model):
> ts_id = models.AutoField(primary_key=True)
> user = models.ForeignKey(Users, db_column='ts_user',
> related_name='ts_user_child')
> project = models.ForeignKey(Projects, db_column='project',
> related_name='project_child', null='true')
> class Meta:
> db_table = 'timesheet'
> 
> I use the following in my Django view to retrieve the data according
> to the filtering argument received:
> filter_str = request.POST['filter_str']
> #filter_str contains something like this:  "project__name = 'Django
> supported web app'"
> Timesheet.objects.filter(user = id).extra(where = [filter_str])
> 
> But it gives me the error:
> 
> column project__name does not exist
> 
> Without using extra the same query works fine so the column does
> exist. 

Your conclusion here is incorrect. There is no column in the database
table called "project__name". When you pass that string to a filter()
call, Django does a lot of processing and works out the right database
table and column name to use for the query. Have a look at the output of

Timesheet.objects.filter(project__name="foo").query.as_sql()

for example. You will not see any reference to a column called
"project__name" there. If you are using extra(), you need to use the
correct column names: extra() only exists as a way to write almost raw
SQL portions.

It sounds a lot like you really want to be using a second filter() call,
not an extra() call.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---