Search user from Database

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Search user from Database

nobody
Hi,

Sorry for asking a new B question. I have following code in a module.py:


class Group(models.Model):
......
 users = models.ManyToManyField(User, blank=True, null=True)
.....

I can add user or delete user by calling Group.users.add(user) or Group.users.remove(user), but how can I check if the user has already in the database?

Thank you.

 -j

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/017b0bc2-ff5c-4a42-936d-00da2b246cbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search user from Database

Filipe Ximenes
Just checking: when you say you are adding users using

Group.users.add(user)

in this case `Group` is an instance of the Group model and NOT the actual model, correct?

if so, you can make a query using the same instance, looking for the user:
user = Group.user.filter(user).first()
if user:
    print('user exists in this group')
else:
    print('user does not exists in this group')

On Thu, Mar 26, 2015 at 11:24 PM, nobody <[hidden email]> wrote:
Hi,

Sorry for asking a new B question. I have following code in a module.py:


class Group(models.Model):
......
 users = models.ManyToManyField(User, blank=True, null=True)
.....

I can add user or delete user by calling Group.users.add(user) or Group.users.remove(user), but how can I check if the user has already in the database?

Thank you.

 -j

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/017b0bc2-ff5c-4a42-936d-00da2b246cbf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
http://www.vinta.com.br

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAA-QWB19Dsbi7cC1XF8nSVvNEkbQm7_gQq84bXZDCTxj%3DBcVzw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Search user from Database

tkdchen


On Friday, March 27, 2015 at 11:10:19 PM UTC+8, Filipe Ximenes wrote:
Just checking: when you say you are adding users using

Group.users.add(user)

in this case `Group` is an instance of the Group model and NOT the actual model, correct?

if so, you can make a query using the same instance, looking for the user:
user = Group.user.filter(user).first()
if user:
    print('user exists in this group')
else:
    print('user does not exists in this group')


QuerySet.exists is better than QuerySet.first
 
On Thu, Mar 26, 2015 at 11:24 PM, nobody <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="jLME1qHGdBkJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">jupit...@...> wrote:
Hi,

Sorry for asking a new B question. I have following code in a module.py:


class Group(models.Model):
......
 users = models.ManyToManyField(User, blank=True, null=True)
.....

I can add user or delete user by calling Group.users.add(user) or Group.users.remove(user), but how can I check if the user has already in the database?

Thank you.

 -j

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jLME1qHGdBkJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">django-users...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="jLME1qHGdBkJ" rel="nofollow" onmousedown="this.href='javascript:';return true;" onclick="this.href='javascript:';return true;">django...@....
Visit this group at <a href="http://groups.google.com/group/django-users" target="_blank" rel="nofollow" onmousedown="this.href='http://groups.google.com/group/django-users';return true;" onclick="this.href='http://groups.google.com/group/django-users';return true;">http://groups.google.com/group/django-users.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/django-users/017b0bc2-ff5c-4a42-936d-00da2b246cbf%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/msgid/django-users/017b0bc2-ff5c-4a42-936d-00da2b246cbf%40googlegroups.com?utm_medium\75email\46utm_source\75footer';return true;" onclick="this.href='https://groups.google.com/d/msgid/django-users/017b0bc2-ff5c-4a42-936d-00da2b246cbf%40googlegroups.com?utm_medium\75email\46utm_source\75footer';return true;">https://groups.google.com/d/msgid/django-users/017b0bc2-ff5c-4a42-936d-00da2b246cbf%40googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href='https://groups.google.com/d/optout';return true;" onclick="this.href='https://groups.google.com/d/optout';return true;">https://groups.google.com/d/optout.



--
  
Filipe Ximenes
+55 (81) 8245-9204
Vinta Software Studio
<a href="http://www.vinta.com.br" target="_blank" rel="nofollow" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fwww.vinta.com.br\46sa\75D\46sntz\0751\46usg\75AFQjCNH8g5JRft1eRTxtA-EaJcgqbZfYVw';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fwww.vinta.com.br\46sa\75D\46sntz\0751\46usg\75AFQjCNH8g5JRft1eRTxtA-EaJcgqbZfYVw';return true;">http://www.vinta.com.br

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/74dfdb39-5854-484d-8093-e0c9088c017a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.