MarkO
MarkO

Reputation: 795

Filtering with Managers

I have a manager (AccountManager), I want to filter on the FK user but the user is linked in the accounts table. I have tried the following .filter(account.user=user), but this give me an error.

What is the correct way of doing this? code below.

Thank you

class AccountManager(models.Manager):
    def for_user(self, user):
        return self.get_query_set().filter(account.user=user)

class Transaction(models.Model):

    account = models.ForeignKey('Account',
                                related_name='transactions')

    amount = models.DecimalField(max_digits=10, decimal_places=1)
    objects = AccountManager()


class Account(models.Model):


    OPEN, FROZEN, CLOSED = 'Open', 'Frozen', 'Closed'
    status = models.CharField(max_length=32, default=OPEN)
    #FK
    user = models.OneToOneField(User)

Upvotes: 0

Views: 28

Answers (1)

catherine
catherine

Reputation: 22808

class AccountManager(models.Manager):
    def for_user(self, user):
        return self.get_query_set().filter(account__user=user)

Upvotes: 1

Related Questions