On 27/05/2012 9:28 PM, Sandeep kaur wrote:
I have a table Amounts which has 2 columns field and other_field.
These columns are filled in such a way that either other_field is null
or other_field = field.
I want to make a database query such that

if field == other_field :
    field = "OTHER"
else:
    field = field

That means the entries in table where 2 columns are equal, there field
should be assigned a string  "OTHER" and other_field should remain as
such.
How can I give such query in shell.

To get a queryset containing all the objects where field == other_field should be something like the following:

from django.db.models import F

Amounts.objects.filter(field = F("other_field"))

to then update all those objects with "OTHER" as the value for field would be:

Amounts.objects.filter(field = F("other_field")).update(field="OTHER")

Note that this won't execute the "else" portion above, but as it is not making any change (field already equals field), I assume this isn't an issue.

Regards,
Michael.

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