English 中文(简体)
我如何使用过滤器在Django ORM的两种不同价值上过滤同一领域?
原标题:How do I use filter() to filter same field on two different values in Django ORM?

我有一个模式(1),有另一个模式(2)。 我有一份来自模型(2)的数值清单,我谨对模型(1)中所有数值的物体进行过滤。

我基本上想这样做:

SomeModel.objects.filter(field1=x OR field1=y OR field1=z)

Is this possible, can t find anything in docs.

最佳回答

如果你想要共同放弃,Q物体应有助于你实现:

from django.db.models import Q
Samplemodel.objects.filter(Q(field1= x ) | Q(field1= y ))

Reflink:

问题回答

您可使用<代码>field1_in=[x , y]做到这一点。

Samplemodel.objects.filter(field1__in=[ x ,  y ])




相关问题
how to insert a infomation on a table in Django

This is my form on models.py class ItemForm(forms.Form): itemname = forms.CharField(max_length=100) itemwording = forms.CharField(max_length=100) notes = forms.CharField() abundance =...

Django auto_now and auto_now_add

For Django 1.1. I have this in my models.py: class User(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) When updating a row I ...

Problem getting started with GeoDjango

As soon as I add "from django.contrib.gis.db import models" instead of "from django.db import models", Django stops recognizing the app and gives this error: Error: App with label location could not ...

How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

django ModelManager inheritance

How do I inherit a ModelManager? class Content(models.Model): name = models.CharField(max_length=255, verbose_name= Name des Blogs ) slug = models.SlugField(max_length=80, blank=True) ...

热门标签