我如何过滤这个?
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,该怎么做?
答案 0 :(得分:2)
if request.user.is_authenticated():
subbases = Sub_base.objects.filter(base__user=request.user)
如果request.user.is_authenticated()
,则此用户已登录,否则用户为Anonymous User