2011-01-05 31 views
0

私は別の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 

私は{{invoices.client_contract_number }}を探している場合、私はクライアントの契約番号を取得ジャンゴに知っています。しかし、特定の請求書を知りたがっているとすれば、どのように顧客名を調べるのですか?請求書にクライアントのforeginキー値がないので、{{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' 

答えて

2

次の2つの間の関係合流従うことができます:ところで

invoice.client_contract_number.client_number.name 

を、あなたのフィールド名が紛らわしいです。 client_contract_numberは数字ではなく、契約です。 client_numberは数字でもなく、クライアントです。ちょうどclient_contractclientと呼んでください。

編集質問更新

後、私はあなたがここで何をしようとしてわかりません。 invoices_listはすべての請求書のクエリセットです。明らかに、クライアント名がのリストのものであるかどうかは分かりません。おそらくあなたのテンプレートで - - おそらくあなたが実際にやってみたいことは反復処理であり、各1名をプリントアウト:

{% for invoice in invoices_list %} 
    Client name: {{ client_contract_number.client_number.name }} 
{% endfor %} 
+0

「client_contract_number」は私にとっては数字のようです。しかし、 'client_number'はしません。 –

+0

@Dominic Roger: 'client_contract_number'という2つのフィールドがあります。 'Invoice'モデルのものはForeignKey、' Contract'のものは整数です。 –

+0

私は前にこのようなことを試みたと思うが、うまくいかなかった。私はそれが私のviews.pyに書かれた方法だと思います。私の見解では何が必要でしょうか? – Shehzad009

0
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)) 

あなたが請求書のリストからクライアント名を取得することはできません。あなたが興味を持っている特定の請求書はありますか?

請求書のインスタンスがある場合、invoice.client_contract_number.client_number.nameを実行できます。しかし、あなたはリストでそれをすることはできません!

ところで、複数の結合をトラバースする場合は、クエリセットにselect_related句が含まれていることを確認してください。あなたはその後、各オブジェクトの3関係クエリを実行し、リストを反復している場合のみ、一つの大きなクエリではなく、潜在的に数百人よりも、フロントを実行していることを確認します

invoices_list = Invoice.objects.select_related(depth=3).all() 

dot(invoice.clientは1ドット以上)があるときはいつでも、select_relatedの使用を強く検討してください。

+0

基本的には、1つの列にすべての請求書IDが表示される表を表示したいとします。すべてのクライアント名を示す別の列になければなりません。ただし、正しい請求書IDは、同じ行の正しいクライアント名と一致する必要があります。 – Shehzad009

+0

@シェア私はあなたがしようとしていることを理解しています。受け入れられた答えは正しいです。あなたは、ドットシンタックスを使用して複数の関係にまたがることによって、請求書を介してクライアント名を取得する必要があります。それはテンプレートで行う必要があります。あなたの意見ではありません。請求書を繰り返し処理するときに、その請求書のクライアント名属性を抽出する必要があります。 select_relatedに関する私のアドバイスはまだ良いアドバイスです。ビューでselect_relatedを使用し、テンプレート内のクライアント名にアクセスします。 –