Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:37:50+00:00 2026-06-01T12:37:50+00:00

I am trying to get all orders for a user in which the user

  • 0

I am trying to get all orders for a user in which the user is either part of the order’s vendor’s representatives or managers.

I’ll explain better with some code. Here is the relevant part of my models.py

class Vendor( models.Model ) :
    managers        = models.ManyToManyField( User, related_name = 'managers'       , limit_choices_to = { 'userprofile__user_types' : 4 } )
    representatives = models.ManyToManyField( User, related_name = 'representatives', limit_choices_to = { 'userprofile__user_types' : 5 }, null = True, blank = True )

class Order( models.Model ) :
    creator  = models.ForeignKey( User, related_name = 'creator' )
    approver = models.ForeignKey( User, related_name = 'approver' )
    vendor   = models.ForeignKey( Vendor, null = True, blank = True )

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 )

To get all the orders that a user created it’s easy:

Order.objects.filter( creator = user.pk )

The reason I couldn’t figure out how to get this one is because I couldn’t figure out how to filter with an OR-clause (manager or representative) or check of existence in a M2M relationship (check if part of managers/representatives list).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:37:52+00:00Added an answer on June 1, 2026 at 12:37 pm

    OK, first of all, to explain those two bits of syntax: to OR in filters, you use Q objects:

    from django.db.models import Q
    results = MyModel.objects.filter(Q(name='daniel') | Q(status='fantastic'))
    

    and to test membership in a ManyToMany, you just use =

    results = MyModel.objects.filter(user__name='daniel')
    

    which means “has a user whose name is daniel” (and not, as you might think, “has only one user, ie daniel”).

    So, to put these together, you can do:

    Order.objects.filter(Q(vendor__managers=my_user) | 
                         Q(vendor__representatives=my_user))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get all the direct reports of a User through Active Directory,
I'm trying to get all users, and order them by a field on another
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
iam trying to get all object's xpath's from loaded page via selenium something similar
Im trying to get all Leads / Contact / Accounts from the database. So
I'm trying to get all property names / values from an Outlook item. I
I am trying to get all the rows that exist in allData but not
I'm trying to get all controls in a winform disabled at the Load event.
I'm trying to get all the input elements from a certain form from jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.