Reputation: 30298
I currently have three classes User
, UserProfile
, and Vendor
. User
is the built-in Django one from from django.contrib.auth.models import User
. The other two are as follow
Here is UserProfile
class UserProfile( models.Model ) :
TYPE_CHOICES = (
( 't', 'tenant' ),
( 'm', 'property manager' ),
( 'o', 'property owner' ),
( 'v', 'vendor' ),
( 'w', 'web user' ),
( 'x', 'other' ),
)
user = models.ForeignKey( User, unique = True )
user_type = models.CharField( max_length = 1, choices = TYPE_CHOICES, default = 't' )
User.profile = property( lambda u : UserProfile.objects.get_or_create( user = u )[ 0 ] )
And here is Vendor
class Vendor( models.Model ) :
def __unicode__( self ) :
return self.name
name = models.CharField( max_length = 135 )
users = models.ManyToManyField( User, null = True, blank = True )
I want to limit the Vendor.users to only User
whose UserProfile.user_type
is vendor
.
How do I use limit_choices_to
. Can I do something like...
users = models.ManyToManyField( User, null = True, blank = True, limit_choices_to = { UserProfile.objects.filter( user = self.users ).user_type : 'm' } )
I know the above code will throw an error, but hopefully you can see what I'm trying to do.
Thanks in advance!
Upvotes: 0
Views: 1348
Reputation: 30298
I was able to do it with
manager = models.ForeignKey( User, related_name = 'manager', limit_choices_to = { 'userprofile__user_type' : 2 } )
where 2 is the primary key (user_type_id
) of property manager in the UserType
class.
Upvotes: 1