[英]How can I make a query by a Django foreignkey description?
if i have this code 如果我有此代码
class Father(models.Model):
Description = models.CharField(max_length=50)
Code = models.IntegerField(max_length=2, unique=True)
def __unicode__(self):
return "%s (%s)" % (self.Description, self.Code)
class Son(models.Model):
Father = models.ForeignKey(Father)
Description = models.CharField(max_length=50)
And I want to create a filter by the select box shown to the user. 我想通过显示给用户的选择框来创建一个过滤器。
Assuming that I have a record in Father like: 假设我在父亲的记录如下:
# | Description | Code
--------------------------
1 | Fred Nichols | 100
In the ForeignKey field should have a HTML content like: 在ForeignKey字段中应具有HTML内容,例如:
<option value="1">Fred NIchols (100)</option>
if I try to query by the field in the son model: 如果我尝试按子模型中的字段查询:
Son.objects.filter(Father__Contains="Fred")
I get an error. 我得到一个错误。 In the documentation of Django the nearest option should be something like:
在Django文档中,最接近的选项应类似于:
Son.objects.filter(Father__Description__contains="Fred")
or for any of the columns data 或用于任何列数据
Son.objects.filter(Q(Father__Description__contains="Fred") | Q(Father__Code__Contains="Fred") )
I'd like to make something the most similar to the user info shown. 我想制作与显示的用户信息最相似的内容。
How could I make a query that could do that? 我该如何进行查询呢?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.