我有一个经理(AccountManager),我想过滤FK用户,但用户在帐户表中链接。我尝试了以下.filter(account.user=user)
,但这给了我一个错误。
这样做的正确方法是什么?代码如下。
谢谢
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)
答案 0 :(得分:1)
class AccountManager(models.Manager):
def for_user(self, user):
return self.get_query_set().filter(account__user=user)