3

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

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.

我究竟做错了什么?

4

1 回答 1

2

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

于 2012-07-09T08:19:24.450 回答