我有另一個Django/python問題。我有一個models.py,看起來像這樣。關於查詢集的Django問題
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}},因爲發票中的客戶端沒有任何foregin鍵值。
編輯: 這是我的觀點
@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'
'client_contract_number'對我來說看起來像一個數字。 'client_number'不會。 – 2011-01-05 10:46:10
@Dominic Roger:有兩個字段叫'client_contract_number'。 「Invoice」模型中的一個是ForeignKey,「Contract」中的是一個整數。 – 2011-01-05 10:49:46
我想我以前嘗試過這樣的東西,但它沒有奏效。我認爲這是我在views.py中寫的。我的觀點需要什麼? – Shehzad009 2011-01-05 10:53:56