开发者

Pass several values in a manager

I was wondering if there a way I can pass two or more variables in a custom manager...there are five variables that come from different views but in the model, I've declared a manager to handle filtering based on one of these variables...I want to have all the variables being considered in the filter query. Is there a way to do this?

class VehicleQuerySet(QuerySet):
    def vehicle_qu开发者_开发知识库ery(self, year):
      return  self.filter(common_vehicle__year__year__exact=year).exclude(status__status='Incoming')

class VehicleManager(models.Manager):
    def get_query_set(self):
      return VehicleQuerySet(self.model)

    def vehicle_query(self, year):
      return self.get_query_set().vehicle_query(year)

Then in the view:

vehicle_query = Vehicle.smart_objects.vehicle_query(year)


I think the code you have given is needlessly complicated. You don't need to define a queryset subclass, as the filtering can and should be done in the manager:

class VehicleManager(models.Manager):
    def vehicle_query(self, year):
      return self.get_query_set().filter(common_vehicle__year__year__exact=year).exclude(status__status='Incoming')

However, I don't really understand your question. You already know how to pass one variable into a manager method, why is passing additional ones any more difficult?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜