[CentralOH] Question > Django > Querys > comparing fields from same record.
John Cassidy
jocassid at gmail.com
Wed Sep 16 17:22:31 CEST 2015
Thanks Eric. I'll have to try that.
On Sep 15, 2015 8:50 PM, "Eric Floehr" <eric at intellovations.com> wrote:
> Also, if you ever want to verify the query that is actually generated, you
> can do:
>
> results = MyTable.objects.filter(foo=F('bar'))
> print (results.query)
>
> Which will output something like:
>
> SELECT ... FROM "MyTable" WHERE "MyTable"."foo" = "MyTable"."bar"
>
> Where ... is the list of columns in MyTable.
>
> -Eric
>
>
> On Tue, Sep 15, 2015 at 5:57 PM, Eric Floehr <eric at intellovations.com>
> wrote:
>
>> Hi John,
>>
>> For that, you'll want to use Django's F() objects. F stands for 'field'
>> (or column). So assuming your Django model for the MyTable table is also
>> called MyTable:
>>
>> from django.db.models import F
>> from yourapp.models import MyTable
>>
>> results = MyTable.objects.filter(foo=F('bar'))
>>
>> More information can be found here:
>> https://docs.djangoproject.com/en/1.7/ref/models/queries/
>>
>> Cheers,
>> Eric
>>
>>
>> On Tue, Sep 15, 2015 at 5:44 PM, John Cassidy <jocassid at gmail.com> wrote:
>>
>>> I'm trying to perform a query like this:
>>>
>>> SELECT *
>>> FROM MyTable
>>> WHERE MyTable.foo = MyTable.bar
>>>
>>> Is there a way to create a where clause like this using the filter
>>> method? If not how could this be done?
>>>
>>> -John Cassidy
>>>
>>> _______________________________________________
>>> CentralOH mailing list
>>> CentralOH at python.org
>>> https://mail.python.org/mailman/listinfo/centraloh
>>>
>>>
>>
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20150916/7a548b7d/attachment.html>
More information about the CentralOH
mailing list