MrM*_*ins 5 django django-models
我有两个表:
class Client(models.Model):
name = models.TextField()
lastname = models.TextField()
class Meta:
managed = False
db_table = 'client'
class Clientreport(models.Model):
id_client_home = models.ForeignKey('Client', models.DO_NOTHING, db_column='id_client_home', related_name='home_id_client_home')
id_client_reported = models.ForeignKey('Client', models.DO_NOTHING, db_column='id_client_reported', related_name='client_id_client_home')
class Meta:
managed = False
db_table = 'clientreport'
Run Code Online (Sandbox Code Playgroud)
我正在尝试构建一个与此类似的查询:
SELECT cr.*, cl.id, cl.name, cl.lastname FROM Clientreport cr INNER JOIN Client cl ON cr.id_client_reported = cl.id
WHERE (LOWER(cl.name) LIKE LOWER('%jo%') OR LOWER(cl.lastname) LIKE LOWER('%jo%') )
Run Code Online (Sandbox Code Playgroud)
我尝试使用: SQL 查询
但是,现在我正在尝试使用 django 来做到这一点。如何使用 django 访问连接模型???
小智 5
您可以使用标准的 Django Queryset 过滤器跨连接查询,__用于遍历这样的关系:
Clientreport.objects.filter(client_id_client_home__name='jo')
Run Code Online (Sandbox Code Playgroud)
client_id_client_home作为related_name从你的Clientreport模型。有关使用相关对象的查询的文档中的更多信息。
要重现LIKE LOWER('%jo%')您可以使用__icontains:
Clientreport.objects.filter(client_id_client_home__name__icontains='jo')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3629 次 |
| 最近记录: |