Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck for multiple values in a M2M relationship in Django
    primarykey
    data
    text
    <p>In English, I want to check whether a user's type is either a viewer, a moderator, or an administrator.</p> <p>Here is the relevant part of my <code>models.py</code></p> <pre><code>class UserType( models.Model ) : name = models.CharField( max_length = 135 ) # id | name #----+------------------ # 1 | tenant # 2 | property manager # 3 | property owner # 4 | vendor manager # 5 | vendor # 6 | viewer # 7 | moderator # 8 | administrator class UserProfile( models.Model ) : user = models.OneToOneField( User ) user_types = models.ManyToManyField( UserType, null = True, blank = True ) </code></pre> <p>By the way, I set <code>user.profile</code> to be the same thing as <code>user.get_profile()</code>.</p> <p>In my <code>views.py</code> code, I want to do the checking. I figured out that</p> <pre><code>[ user_type.pk for user_type in user.profile.user_types.all() ] </code></pre> <p>will give me a list of <code>pk</code>'s for the <code>user</code>'s <code>user_type</code>, like so <code>[ 1, 2, 6 ]</code>. Meaning that this particular user is a tenant (1), property manager (2), and a viewer (6).</p> <p>If I just want to check one <code>user_type</code>, then I can simply do</p> <pre><code>if 6 in [ user_type.pk for user_type in user.profile.user_types.all() ] : # This user is a viewer </code></pre> <p>But how can I check multiple user_types/pks? I wanted to do something like</p> <pre><code># This won't work if [ 6, 7, 8 ] in [ user_type.pk for user_type in user.profile.user_types.all() ] : # This user is either a viewer, moderator, or administrator </code></pre> <p>Also is my method of list comprehension to check <code>user_types</code> the <strong>Django-way</strong>? It doesn't seem like it, but I couldn't figure out how to query it <em>cleanly</em> in Django.</p> <p>Any tips and suggestions welcomed. Thanks in advance!</p> <p><strong>EDIT:</strong></p> <p>I just figured out I can list pks the more Django-way with <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#values-list" rel="nofollow"><code>values_list</code></a></p> <pre><code>user_types = user.profile.user_types.values_list( 'pk', flat = True ) # [ 1, 2, 6 ] </code></pre> <p>I also figured out that I <em>could</em> check multiple values like this</p> <pre><code>if len( set( [ 1, 9 ] ).intersection( set( user_types ) ) ) # True because of 1 is in user_types (don't care about 9) if len( set( [ 4, 99 ] ).intersection( set( user_types ) ) ) # False because 4 nor 99 is in user_types </code></pre> <p>But even this <code>set</code> method doesn't seem very Django-friendly. There must be an easier way right?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload