【问题标题】:Django rest api filtering error message: django.core.exceptions.FieldError: Related Field got invalid lookupDjango rest api 过滤错误信息:django.core.exceptions.FieldError: Related Field got invalid lookup
【发布时间】:2018-08-17 14:40:19
【问题描述】:

我正在尝试使用包含在 MainName 中的名称查询我的数据库,这将使用此查询过滤 Profile

Profile.objects.all().filter(simmatch__mainname__mainname=name.lower())

但我收到此错误消息:

django.core.exceptions.FieldError: Related Field got invalid lookup: mainname

有人能告诉我我做错了什么吗?我对 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")

【问题讨论】:

    标签: django django-models django-rest-framework


    【解决方案1】:

    将您的查询更改为:

    Profile.objects.all().filter(simmatch__mainName__mainName="My Name")
    

    您不应该使用小写的显式字段。

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2017-11-08
      • 2015-02-14
      • 2021-09-15
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多