road
road

Reputation: 479

Django rest api filtering error message: django.core.exceptions.FieldError: Related Field got invalid lookup

I am trying to query my database with name contains in the MainName which will filter Profile with this query :

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

But I get this error message:

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

Could someone tell me what I am doing wrong ? I am fairly new to Django. Thanks!

My models:

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)

My query:

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

Upvotes: 0

Views: 569

Answers (1)

Ehsan Nouri
Ehsan Nouri

Reputation: 2040

change your query to:

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

you should not use your explicit fields in lowercase.

Upvotes: 1

Related Questions