Django中的QuerySet - 返回异常

时间:2015-02-14 20:50:32

标签: django django-queryset

我试图理解Django的查询是如何工作的,我按照教程不行,我不知道我做错了什么。

当我跑步时

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

它返回

“选择是%s”%s(名称,“,”。join(available)))django.core.exceptions.FieldError:无法解析关键字“nome into field。选择是: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

提前致谢

2 个答案:

答案 0 :(得分:1)

如果使用BeneficientPager,则需要执行

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

答案 1 :(得分:0)

您收到错误是因为nomeBeneficiente上的字段,而不是BeneficientePagar

你可以做

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

将返回Beneficiente s的查询集。或者,如果您需要BeneficientePagar,则可以通过外键查询。

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