我想OR在过滤对象数据时在 Django 中使用带条件的过滤器。
我有一个类的对象,我有 3 或 4 个字段,在此基础上我想OR在 Django 中使用条件过滤数据。
例如。
Obj = Books.objects.filter(title=title or price=price or description=description or author=author)
Run Code Online (Sandbox Code Playgroud)
我认为这不是执行过滤的正确方法。
OR在我的 django 过滤器中使用条件的正确方法是什么
from django.db.models import Q
Obj = Books.objects.filter(Q(title=title) | Q(price=price) | Q(description=description) | Q(author=author))
Run Code Online (Sandbox Code Playgroud)
文档:http : //docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects 来源:https : //stackoverflow.com/a/739922/4808939
| 归档时间: |
|
| 查看次数: |
5815 次 |
| 最近记录: |