[CentralOH] Question > Django > Querys > comparing fields from same record.

Eric Floehr eric at intellovations.com
Wed Sep 16 02:50:09 CEST 2015


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
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20150915/b1f74781/attachment.html>


More information about the CentralOH mailing list