tags:

views:

254

answers:

2

There must be a problem with super(InviteManager, self).get_query_set() here but I don't know what to use. When I look through the RelatedManager of a user instance,

len(Invite.objects.by_email()) == len(user.invite_set.by_email())

Even if the user does not have any invites. However, user.invite_set.all() correctly returns all of the Invite objects that are keyed to the User object.

class InviteManager(models.Manager):
    ''' with this we can get the honed querysets like user.invite_set.rejected '''

    use_for_related_fields = True

    def by_email(self):
        return super(InviteManager, self).get_query_set().exclude( email='' )

class Invite(models.Model):
    ''' an invitation from a user to an email address ...'''

    user = models.ForeignKey('auth.User', related_name='invite_set')
    email = models.TextField( blank=True, )
    objects = InviteManager()

'''
u.invite_set.by_email() returns everything that Invite.objects.by_email() does
u.invite_set.all() properly filters Invites and returns only those where user=u
'''
A: 

Try:

def by_email(self):
    return super(InviteManager, self).exclude(email='')

If nothing else, the .get_query_set() is redundant. In this case, it may be returning a whole new queryset rather than refining the current one.

SmileyChris
A: 

You may want a custom QuerySet that implements a by_email filter. See examples on Subclassing Django QuerySets.

class InviteQuerySet(models.query.QuerySet):
    def by_email(self):
       return self.exclude(email='')

class InviteManager(models.Manager):
    def get_query_set(self):
        model = models.get_model('invite', 'Invite')
        return InviteQuerySet(model)
tcarobruce