Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django | Models where clause

Tags:

django

How to construct a where clause using Django models:

insert in to tablename where email=emailaddress

Thanks.

like image 236
Switch Avatar asked May 18 '10 20:05

Switch


2 Answers

I think you are rather looking for a possibility to UPDATE an existing object.

obj=MyModel.objects.get(email=emailaddress)
obj.name = 'xxxx'
obj.save()
like image 65
Bernhard Vallant Avatar answered Oct 08 '22 15:10

Bernhard Vallant


In case the email address is not unique, which is strange but let's suppose that, you should use the filter method and loop on result

users = MyObject.objects.filter(email=emailaddress)
for u in users:
    # your change here
    u.is_superuser = True
    u.save()
like image 35
Wael Ben Zid El Guebsi Avatar answered Oct 08 '22 15:10

Wael Ben Zid El Guebsi