0

I am trying to understand how exactly query works on Django, i followed the tutorials it´s not working I am not sure what i am doing wrong.

When I run

BeneficientePagar.objects.filter(nome__contains="Joao Pedro") 

it returns

"Choices are %s" %s (name, ",".join(available))) django.core.exceptions.FieldError: Cannot resolve keyword "nome into field. Choices are: ID, beneficiente, beneficiente_id,join, join_id, moeda

from django.db import models

# Create your models here.

class Moeda(models.Model):
    moeda_ficticia = models.FloatField()

class Join(models.Model):
    nome = models.CharField(max_length=150)
    nascimento = models.DateField()
    cpf = models.IntegerField(primary_key=True)
    endereco = models.CharField(max_length=150)
    email = models.EmailField()

    def __str__(self):
        return self.nome

class Beneficiente(models.Model):
    ID = models.AutoField(primary_key=True)
    nome = models.CharField(max_length=150)
    CNPJ = models.IntegerField(max_length = 10)
    def __str__(self):
        return self.nome

class Favores(models.Model):
    ID = models.AutoField(primary_key=True)
    favor = models.CharField(max_length=150)
    dataInserido = models.DateField()
    usuarios = models.ForeignKey(Join)
    def __str__(self):
        return self.favor

class BeneficientePagar(models.Model):
    ID = models.AutoField(primary_key=True)
    moeda = models.IntegerField()
    beneficiente = models.ForeignKey(Beneficiente)
    join = models.ForeignKey(Join)
    def __str__(self):
        return self.ID

Thanks in advance

2 Answers 2

1

If using BeneficientPager, you need to do

BeneficientePagar.objects.filter(beneficient__nome__contains="Joao Pedro") 
Sign up to request clarification or add additional context in comments.

Comments

0

You are getting the error because nome is a field on Beneficiente, not BeneficientePagar.

You can either do

Beneficiente.objects.filter(nome__contains="Joao Pedro") 

which will return a queryset of Beneficientes. Or if you need BeneficientePagar you can query through the foreign key.

BeneficientePagar.objects.filter(beneficiente__nome__contains="Joao Pedro")

6 Comments

Weird, it doens´t give me an error, but it returns "[ ]". I thought it would return the informations available for this name("Joao Pedro"). And if I edit to: BeneficientePagar.objects.filter(join__nome__contains="Joao Pedro") it returns the object that is on BeneficientePagar. Which is weird since i thought it would return the name that is one Join table.
BeneficientePagar.objects.filter() always returns BeneficientePagar objects. If the query beneficiente__nome__contains="Joao Pedro" returns [], then there are no BeneficientePagar objects linked to a Beneficiente with nome containing "Joao Pedro". When you do the filter join__nome__contains="Joao Pedro" it returns the BeneficientePagar objects linked to a Join with name Joao Pedro".
Oww i got it. I think I am making a big confusion. What I really wanted to do is acessing the information via Foreginkey. I thought that if I could acess the data using the "BeneficientePagar" class, I mean, acessing the data in Class Join and Beneficiente. I looked up the docs on django and I thought that was it. Any other tips? Thanks
I'm not sure what you're trying to do. If you have a BeneficientePagar instance beneficiente_pagar, you can access the related beneficiente with beneficiente_pagar.beneficiente.
Well I wanted to have acess from the the BeneficientePagar instance and acess the name ("nome" in my models) which is in my Join class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.