Reputation: 639
Consider my model:
class CampUserInvitation(models.Model):
user = models.ForeignKey('auth.User')
camp = models.ForeignKey('events.Camp')
user_accepted = models.BooleanField(default=False)
This model above is an intermediary model for m2m relationship between Camp and User:
class Camp(Event):
invited = models.ManyToManyField('auth.User',
through='events.CampUserInvitation')
def get_users_signed(self):
return (self.invited
.filter(user_accepted__exact=True)
.filter(user_accepted_timestamp__lte=self.invitation_deadline)
)
when I want to filter the queryset of all Users by only those that have user_accepted field set to True (in the above get_users_signed method)
self.invited.filter(user_accepted__exact=True)
I get:
Cannot resolve keyword 'user_accepted' into field. Choices are: <bunch of attributes of the User class>
Why does django think that 'accepted' is a parameter of the user? Wasn't I supposed to use double underscore for that?
Upvotes: 0
Views: 1551
Reputation: 600051
That's not what's happening at all.
invited
is a reference to the User model, so when you filter on invited, that's the model you filter on. If you need to filter on CampUserInvitation, you need to use the reverse relationship from the ForeignKey on that model:
self.campuserinvitation_set.filter(user_accepted=True)
Edit after comment This might work:
self.invited.filter(campuserinvitation__user_accepted=True)
Upvotes: 2