简体   繁体   English

Django:get_queryset(self)中的条件表达式

[英]Django : Conditional expressions in get_queryset(self)

I am in Django 1.11 and I would like to combine what I read: 我在Django 1.11中,我想结合阅读的内容:

For example, suppose I have something like this that will check if Objects are in the user area and a ListView use it. 例如,假设我有类似这样的东西,它将检查对象是否在用户区域中,并且ListView使用它。

    open_help_requests = HelpRequest.objects.filter(state=HelpRequest.OPEN)
    filtered_help_requests = []
    for help_request in open_help_requests:
        """ Formule de Haversine pour connaitre la distance entre deux points géographiques """
        earth_radius = 6373.0

        lat1 = radians(help_request.student.studentcollaborator.postal_code.latitude)
        lon1 = radians(help_request.student.studentcollaborator.postal_code.longitude)
        lat2 = radians(request.user.student.studentcollaborator.postal_code.latitude)
        lon2 = radians(request.user.student.studentcollaborator.postal_code.longitude)

        dlon = lon2 - lon1
        dlat = lat2 - lat1

        a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))

        distance = earth_radius * c
        if distance <= help_request.settings.distance:
            filtered_help_requests.append(help_request)

What I want to move this condition check inside the filter in def get_queryset(self): so that I could directly make additional simple order/filter operations with the filtered QuerySet. 我想在def get_queryset(self)中过滤器内部移动此条件检查的内容这样,我就可以使用过滤后的QuerySet直接进行其他简单的订单/过滤器操作。 Recreate the QuerySet with the filtered variable list id looks too heavy (like this : Django get a QuerySet from array of id's in specific order ). 用过滤后的变量列表id重新创建QuerySet看起来太沉重了(像这样: Django从id数组中以特定顺序获取QuerySet )。

Any ideas ? 有任何想法吗 ?

Thanks. 谢谢。

Quite not sure that I'm understanding you well. 不太确定我对您的理解很好。 Anyway,what about creating custom models.QuerySet for your models models.Manager ? 无论如何,如何为模型models.Manager创建自定义models.QuerySet

Inside your custom models.QuerySet you can create your own functions for your ordering/filtering goals. 在您的自定义models.QuerySet您可以为自己的排序/过滤目标创建自己的函数。

And in your view you can directly get your filtered/ordered list of your objects. 在您的视图中,您可以直接获取对象的过滤/排序列表。

class Foo(models.Model):
    ....
    objects = FooManager()
    ....

class FooManager(models.Manager):
    ....
    def get_queryset(self):
        return FooQuerySet(self.model, using=self._db)
    ....

class FooQuerySet(models.QuerySet):
    ....
    # define your functions here
    # just a simple example
    def order(self, *args, **kwargs):
        return self.filter(
            # you may use Q objects here or whatever 
            ).distinct()
        else:
           return self
    ....

And in your view: 在您看来:

class BarView(ListView):
    ....
    def get_queryset(self):
        ....
        return Foo.objects.all().order(params) # your custom function
        ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM