Django 关于查询集的问题
我还有另一个 Django/python 问题。我有一个 models.py ,看起来像这样。
class Client(models.Model):
client_number = models.PositiveIntegerField()
name = models.CharField(max_length=80)
address = models.CharField(max_length=250)
telephone = models.CharField(max_length=20)
fax = models.CharField(max_length=20)
email = models.EmailField()
alternative_name = models.CharField(max_length=80, blank=True, null=True)
alternative_address = models.CharField(max_length=250, blank=True, null=True)
alternative_telephone = models.CharField(max_length=20, blank=True, null=True)
alternative_email = models.EmailField(blank=True, null=True)
def __unicode__(self):
return unicode(self.client_number)
class Contract(models.Model):
client_number = models.ForeignKey(Client)
client_contract_number = models.PositiveIntegerField()
start_date = models.DateField()
end_date = models.DateField()
contract_type = models.IntegerField(verbose_name = "Contract Types", choices = CONTRACT_TYPE_CHOICES)
contract_status =models.IntegerField(verbose_name = "Contract Status", choices = CONTRACT_STATUS_CHOICES)
exception = models.DecimalField(max_digits=5, decimal_places=2)
uplift_percentage = models.DecimalField(max_digits=5, decimal_places=2)
payment_day = models.DateField()
payment_type = models.IntegerField(verbose_name = "Payment Type", choices = PAYMENT_TYPE_CHOICES)
late_payment = models.IntegerField(verbose_name = "Late Payment Change", choices = LATE_PAYMENT_CHOICES)
late_payment_change_rate = models.DecimalField(max_digits=5, decimal_places=2)
contract_value = models.DecimalField(max_digits=20, decimal_places=2)
monthly_value = models.DecimalField(max_digits=20, decimal_places=2)
def __unicode__(self):
return unicode (self.client_contract_number)
class Invoice(models.Model):
transaction_type = models.IntegerField(verbose_name = "Transaction type", choices = TRANSACTION_TYPE_CHOICES)
invoice_number = models.CharField(max_length=16)
date = models.DateField()
client_contract_number = models.ForeignKey(Contract)
invoice_contact = models.CharField(max_length=80)
invoice_net = models.DecimalField(max_digits=16, decimal_places=2)
invoice_vat = models.DecimalField(max_digits=16, decimal_places=2)
invoice_gross = models.DecimalField(max_digits=16, decimal_places=2)
payment_date = models.DateField()
special_notes = models.CharField(max_length=128)
def __unicode__(self):
return self.invoice_number
我知道在 django 中如果我查找 {{invoices.client_contract_number }}
,我会得到客户合同号。但假设我想知道特定发票,我将如何查找客户姓名?我无法执行 {{invoice.name}},因为发票中没有客户的外键值。
编辑: 这是我的观点
@login_required
def homepage(request):
invoices_list = Invoice.objects.all()
invoice_name = invoices_list.client_contract_number.client_number.name
return render_to_response(('index.html', locals()), {'invoices_list': invoices_list }, context_instance=RequestContext(request))
和错误。
'QuerySet' object has no attribute 'client_contract_number'
I have another Django/python question. I have a models.py which look something like this.
class Client(models.Model):
client_number = models.PositiveIntegerField()
name = models.CharField(max_length=80)
address = models.CharField(max_length=250)
telephone = models.CharField(max_length=20)
fax = models.CharField(max_length=20)
email = models.EmailField()
alternative_name = models.CharField(max_length=80, blank=True, null=True)
alternative_address = models.CharField(max_length=250, blank=True, null=True)
alternative_telephone = models.CharField(max_length=20, blank=True, null=True)
alternative_email = models.EmailField(blank=True, null=True)
def __unicode__(self):
return unicode(self.client_number)
class Contract(models.Model):
client_number = models.ForeignKey(Client)
client_contract_number = models.PositiveIntegerField()
start_date = models.DateField()
end_date = models.DateField()
contract_type = models.IntegerField(verbose_name = "Contract Types", choices = CONTRACT_TYPE_CHOICES)
contract_status =models.IntegerField(verbose_name = "Contract Status", choices = CONTRACT_STATUS_CHOICES)
exception = models.DecimalField(max_digits=5, decimal_places=2)
uplift_percentage = models.DecimalField(max_digits=5, decimal_places=2)
payment_day = models.DateField()
payment_type = models.IntegerField(verbose_name = "Payment Type", choices = PAYMENT_TYPE_CHOICES)
late_payment = models.IntegerField(verbose_name = "Late Payment Change", choices = LATE_PAYMENT_CHOICES)
late_payment_change_rate = models.DecimalField(max_digits=5, decimal_places=2)
contract_value = models.DecimalField(max_digits=20, decimal_places=2)
monthly_value = models.DecimalField(max_digits=20, decimal_places=2)
def __unicode__(self):
return unicode (self.client_contract_number)
class Invoice(models.Model):
transaction_type = models.IntegerField(verbose_name = "Transaction type", choices = TRANSACTION_TYPE_CHOICES)
invoice_number = models.CharField(max_length=16)
date = models.DateField()
client_contract_number = models.ForeignKey(Contract)
invoice_contact = models.CharField(max_length=80)
invoice_net = models.DecimalField(max_digits=16, decimal_places=2)
invoice_vat = models.DecimalField(max_digits=16, decimal_places=2)
invoice_gross = models.DecimalField(max_digits=16, decimal_places=2)
payment_date = models.DateField()
special_notes = models.CharField(max_length=128)
def __unicode__(self):
return self.invoice_number
I know in django if I look for {{invoices.client_contract_number }}
, I get the client contract number. But supposing I wanted to know for a particular invoice, how would I look up the clients name is? I cannot do {{invoice.name}}, because there is no foregin key value for client in invoice.
Edit:
Here is my views
@login_required
def homepage(request):
invoices_list = Invoice.objects.all()
invoice_name = invoices_list.client_contract_number.client_number.name
return render_to_response(('index.html', locals()), {'invoices_list': invoices_list }, context_instance=RequestContext(request))
And error.
'QuerySet' object has no attribute 'client_contract_number'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以跟踪两个连接之间的关系:
顺便说一句,您的字段名称令人困惑。
client_contract_number
不是数字,而是合同。client_number
也不是数字,它是一个客户端。只需将它们称为client_contract
和client
即可。问题更新后编辑
我不确定您想在这里做什么。
invoices_list
是所有发票的查询集 - 显然询问该列表的客户名称是没有意义的。想必您真正想要做的是迭代 - 可能在您的模板中 - 并打印出每个的名称:You can follow the relationship across two joins:
By the way, your field names are confusing.
client_contract_number
isn't a number, it's a Contract. Andclient_number
isn't a number either, it's a Client. Just call themclient_contract
andclient
.Edit after question update
I'm not sure what you're trying to do here.
invoices_list
is a queryset of all invoices - obviously it doesn't make sense to ask what the client name is for that list. Presumably what you actually want to do is iterate through - probably in your template - and print out the name for each one:您无法从发票列表中获取客户名称。有您感兴趣的特定发票吗?
当您有发票实例时,您可以执行
invoice.client_contract_number.client_number.name
。但你不能在清单上做到这一点!顺便说一句,如果您要遍历多个联接,请确保您的查询集具有
select_lated
子句。这将确保只预先执行一个大查询,而不是在迭代列表时可能执行数百个查询,然后对每个对象执行 3 个关系查询。每当您有超过 1 个点(invoice.client 是一个点)时,请强烈考虑使用 select_lated。
You can't get the client name from a LIST of invoices. Is there a particular invoice that you're interested in?
When you have an instance of an invoice, then you can do
invoice.client_contract_number.client_number.name
. But you can't do it on a list!By the way, if you're going to be traversing multiple joins, make sure your query set has a
select_related
clause.That will ensure that only one big query is executed up front, rather than potentially hundreds if you're iterating through a list, then doing a 3 relation query for each object. Any time you have more than 1
dot
(invoice.client is one dot), strongly consider using select_related.