如何过滤用户相关的外键?

时间:2014-12-20 08:24:23

标签: python django

我如何过滤这个?

class Base(models.Model):
    user = models.ForeignKey(User)
    base_name = models.CharField(max_length=100)

class Sub_base(models.Model):
    base = models.ForeignKey(Base)
    sub_base_name = models.CharField(max_length=100)

现在我只想根据当前登录用户过滤Sub_base,该怎么做?

1 个答案:

答案 0 :(得分:2)

if request.user.is_authenticated(): 
   subbases = Sub_base.objects.filter(base__user=request.user)

如果request.user.is_authenticated(),则此用户已登录,否则用户为Anonymous User