Vivek Sable
Vivek Sable

Reputation: 10221

SQL command to update column value in table

Following is code on python manage shell

>>> User.objects.filter(email__icontains="gmail.com").values_list("email", flat=True)
[u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]']
>>> for ii in User.objects.filter(email__icontains="gmail.com"):
...      ii.email = ii.email.replace("@gmail.com", "@custom.com")
...      ii.save() 
...     
... 
>>> User.objects.filter(email__icontains="gmail.com").values_list("email", flat=True)
[]
>>> User.objects.filter(email__icontains="@custom.com").values_list("email", flat=True)
[u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]', u'[email protected]']
>>>

I want to write SQL command in Postgresql terminal (python manage dbshell)

How can I convert above in SQL command?

Following are my try:

[Edited1]:

Get Target email ids by SQL command:

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
           email           
---------------------------
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
(5 rows)

dp=# 

Upvotes: 2

Views: 375

Answers (3)

Vivek Sable
Vivek Sable

Reputation: 10221

Following is my solution:

UPDATE auth_user Set email = replace(email, '@gmail.com', '@custom.com') where email LIKE '%@gmail.com';

Demo:

Go into dbshell 1. cd /var/op/project_name python manage dbshell

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
           email           
---------------------------
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
(5 rows)

dp=# SELECT  email FROM auth_user where email LIKE '%@custom.com';
 email 
-------
(0 rows)

dp=# UPDATE auth_user Set email = replace(email, '@gmail.com', '@custom.com') where email LIKE '%@gmail.com';
UPDATE 5

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
 email 
-------
(0 rows)

dp=# SELECT  email FROM auth_user where email LIKE '%@custom.com';
           email            
----------------------------
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
(5 rows)

dp=# 

Upvotes: 0

bakkal
bakkal

Reputation: 55448

How can I convert above in SQL command?

You can have a look at the query Django generates for this, it might not be runnable as in (e.g. missing params that are sent by Django), but it'll give you a good idea of how Django translates it to SQL

The idea is to print this value: Model.objects.filter(...).values_list(...).query

query = User.objects.filter(email__icontains="@custom.com").values_list("email", flat=True).query

# Make it print it
print query 
print(query) # Python 3 or with "from future import print_function"

Upvotes: 3

jazgot
jazgot

Reputation: 2073

So you want to replace domain in emails, here is test select:

select email, replace(email, '@gmail.com', '@custom.com') as new_email 
from auth_user 
where email like '%@gmail.com';

And update will be:

update auth_user 
set email = replace(email, '@gmail.com', '@custom.com') 
where email like '%@gmail.com';

Upvotes: 1

Related Questions