Django Model Manager相关过滤器

时间:2012-07-09 08:14:30

标签: django django-models

我想过滤经理中的相关条目:

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

class TravelsCars(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=255)
    ...
    objects = UserTravelsCarsManager()

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

class Travels(models.Model, ClonableMixin):
    user = models.ForeignKey(User)
    vehicle = models.ForeignKey(TravelsCars)
    ...

    objects = UserTravelsManager()

它本身不会起作用。我得到了所有用户的所有汽车。我试过了:

return super(UserTravelsManager, self).get_query_set().filter(user=user, vehicle__user=user)

这也行不通。

更新: 要清楚Travels的条目已被过滤。如果我通过TravelsCars查询,则只会过滤相关的Travels

我做错了什么?

1 个答案:

答案 0 :(得分:2)

而不是super(UserTravelsCarsManager, self).get_query_set().filter...尝试使用self.filter(user=user)UserTravelsManager

中相同