简体   繁体   English

如何在 Django 查询集上创建默认过滤器

[英]How to create default filter on django queryset

I have below model in my django app:我的 django 应用程序中有以下模型:

class Segment(BaseSegmentModel):
    payload_json = JSONField(null=True, default=None, blank=True)
    owner = models.ForeignKey(CustomUser, null=True, blank=True, on_delete=models.SET_NULL, related_name='segments')
    name = models.CharField(max_length=100, null=True, blank=False)
    status = models.CharField(max_length=100, null=True, blank=True, choices=SegmentStatuses.choices, default=SegmentStatuses.STOPPED)
    created_date = models.DateTimeField(null=True, auto_now_add=True)

There is a field status which has three values:有一个字段状态,它具有三个值:

class SegmentStatuses:
    STOPPED = 'stopped'
    ACTIVE = 'active'
    ARCHIVE = 'archive'

    choices = ((STOPPED, STOPPED), (ACTIVE, ACTIVE), (ARCHIVE, ARCHIVE))

I need to add a default filter on this model where it should not show any results with status = Archive .我需要在这个模型上添加一个默认过滤器,它不应该显示任何带有status = Archive的结果。

So whenever someone query this model it should not show any Archive results.因此,每当有人查询此模型时,它都不应显示任何存档结果。

You can have 2 managers for your model: one for all objects and one for excluded objects您的模型可以有 2 个管理器:一个用于所有对象,一个用于排除对象

class SegmentStatuses:
    STOPPED = 'stopped'
    ACTIVE = 'active'
    ARCHIVE = 'archive'

    choices = ((STOPPED, STOPPED), (ACTIVE, ACTIVE), (ARCHIVE, ARCHIVE))

class UnArchivedManager(models.Manager):
    def get_queryset(self):
        return super(UnArchivedManager, self).get_queryset().exclude(status=SegmentStatuses.ARCHIVE)


class Segment(BaseSegmentModel):
    payload_json = JSONField(null=True, default=None, blank=True)
    owner = models.ForeignKey(CustomUser, null=True, blank=True, on_delete=models.SET_NULL, related_name='segments')
    name = models.CharField(max_length=100, null=True, blank=False)
    status = models.CharField(max_length=100, null=True, blank=True, choices=SegmentStatuses.choices, default=SegmentStatuses.STOPPED)
    created_date = models.DateTimeField(null=True, auto_now_add=True)
    
    objects = UnArchivedManager()
    objects_all = models.Manager()

As you can in this image, I just have only one object in DB and it has 'archive' status, so when I used the 'object' manager I didn't see any objects in the result, but when I used the 'object_all' manager I saw 1 object in the result正如您在这张图片中看到的那样,我在 DB 中只有一个对象并且它具有“归档”状态,所以当我使用“对象”管理器时,我没有在结果中看到任何对象,但是当我使用“object_all”时' 经理我在结果中看到了 1 个对象

example queries示例查询

If you don't want to have access to archived status at all, you can ignore the objects_all manager.如果您根本不想访问归档状态,则可以忽略objects_all管理器。

In the view you can do it using exclude() :在视图中,您可以使用exclude()来做到这一点:

views.py:视图.py:

Segment.objects.exclude(status='archive')

Edit:编辑:

You can override the get_queryset of manager.您可以覆盖管理器的get_queryset

An Example:一个例子:

models.py模型.py

class AnyManager(models.Manager):
    def get_queryset(self):
        qs = super().get_queryset()
        return qs.exclude(status='archive')

class Segment(BaseSegmentModel):
    payload_json = JSONField(null=True, default=None, blank=True)
    owner = models.ForeignKey(CustomUser, null=True, blank=True, on_delete=models.SET_NULL, related_name='segments')
    name = models.CharField(max_length=100, null=True, blank=False)
    status = models.CharField(max_length=100, null=True, blank=True, choices=SegmentStatuses.choices, default=SegmentStatuses.STOPPED)
    created_date = models.DateTimeField(null=True, auto_now_add=True)

    objects = AnyManager()

It will exclude the fields which have status='archive' , so you can now directly do it as:它将排除具有status='archive'的字段,因此您现在可以直接执行以下操作:

views.py:视图.py:

Segment.objects.all()

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

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