Custom filter backend or Django-Filter?

huangapple go评论51阅读模式
英文:

Custom filter backend or Django-Filter?

问题

  1. 创建自定义过滤器后端以实现筛选和搜索逻辑。
  2. 使用 Django-Filter 包执行此任务。

这是我现在的代码的一部分:

from django.db import models
from rest_framework import viewsets, filters

class MyModel(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

class MyModelViewSet(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    filter_backends = [filters.SearchFilter]
    search_fields = ['title', 'description']

我只是想知道,哪种方法在性能、易于维护和遵循最佳实践方面更好?您能告诉我两种方法的优缺点以及可能出现的问题吗?例如,如果我创建自定义过滤器后端,如何确保它良好?如果我选择 Django-Filter,有哪些问题或限制我应该了解?

非常感谢您的任何帮助和建议!

英文:

I work on simple Django project that uses DRf and I have small problem. I want to add filtering and searching to my API endpoints, but I don't know which way is better:

  1. Make custom filter backend for filtering and searching logic.
  2. Use Django-Filter package for this task.

Here is part of my code now:

from django.db import models
from rest_framework import viewsets, filters

class MyModel(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

class MyModelViewSet(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    filter_backends = [filters.SearchFilter]
    search_fields = ['title', 'description']

I am just wondering, which method is better for performance, easy to maintain, and follows best practices?
Can you tell me pros and cons for both methods and any problems I can have? For example, if I make custom filter backend, how to make it good? If I choose Django-Filter, are there problems or limits I should know?

I am very thankful for any help and advice!

答案1

得分: 1

根据你的情况而定。大多数情况下,我会选择使用维护的包。这样,如果出现错误或更新,包将在需要时进行更新。当你需要高度特定的筛选时,大多数情况下编写自己的代码更好。

对于性能,大多数情况下,你应该使用易于实施且快速的方法。当你的应用程序增长时,你会自动看到哪些部分太慢,然后可以有针对性地修复这些部分。这样可以保持项目的动力。黄金法则是开发速度和可维护性应该优先于性能。只有当性能成为问题时,你应该更新那部分。

英文:

Depends on your situation. Most of the time I would go with a maintained packege. This way if there are bugs or updates, the package will be updated when needed to. When you need highly specific filtering, most of the time writing your own is better.

For performence, most of the time you should work with what is easy and fast to implement. When your application grows, you will automaticly see what parts are to slow, and you can fix these part specificly. This way you keep momentum in your project. Golden rule is that speed of development and maintainablitiy should have priority over performence. Only when performence becomes an issue, you should update that part.

huangapple
  • 本文由 发表于 2023年3月31日 20:05:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定