django - Django rest api 过滤错误信息:django.core.exceptions.FieldError: Related Field got invalid lookup
问题描述
我正在尝试使用名称查询我的数据库,其中包含将使用此查询MainName
进行过滤:Profile
Profile.objects.all().filter(simmatch__mainname__mainname=name.lower())
但我收到此错误消息:
django.core.exceptions.FieldError:相关字段查找无效:主名称
有人可以告诉我我做错了什么吗?我对 Django 很陌生。谢谢!
我的模型:
class Profile(models.Model):
ID = models.IntegerField(unique=True, primary_key=True)
name = models.CharField(max_length=200)
hasArticle = models.CharField(max_length=3)
gender_guessed = models.CharField(max_length=5)
age = models.CharField(max_length=5)
profile = models.TextField()
def __str__(self):
return "{}".format(self.name)
class MainName(models.Model):
ID = models.IntegerField(unique=True, primary_key=True)
mainName = models.CharField( max_length=100)
def __str__(self):
return "{}".format(self.mainName)
class SimMatch(models.Model):
profile = models.ForeignKey(Profile,to_field="ID", db_column="profile_ID",on_delete=models.CASCADE,)
mainName = models.ForeignKey(MainName, to_field="ID", db_column="ID",on_delete=models.CASCADE,)
def __str__(self):
return "{}-{}".format(self.mainName,self.profile)
我的查询:
Profile.objects.all().filter(simmatch__mainname__mainname="My Name")
解决方案
将您的查询更改为:
Profile.objects.all().filter(simmatch__mainName__mainName="My Name")
你不应该使用小写的显式字段。
推荐阅读
- php - 引导数据表类型错误:d 未定义
- python - 为这个分而治之的解决方案添加记忆
- c# - GetResponseAsync 卡在 Scheduled 状态
- javascript - Vue.js 文本输入不显示 v-model 的值
- .net - id_token/Identity token 声明隐式与任何其他流之间的差异
- python - 为什么 Django FieldFIle readline() 返回文本文件的十六进制版本?
- flutter - 在 Flutter 中使用 Stack 和 Flexible Positioner 进行布局?
- list - SwiftUI:NavigationView 中列表的导航标题卡在适当的位置(滚动动画损坏)
- python - Python Selenium 查找独占对象属性
- android - Firebase 崩溃,没有线索