2

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

3 Answers 3

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, Up-voted. I add my answer too can you review by answer?
1

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

1 Comment

@Thank you, Up-voted. I add my answer too can you review my answer?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.