2011-01-05 4 views
0

다른 장고/파이썬 질문이 있습니다. 내가 이런 식으로 보이는 models.py 있어요. 내가 {{invoices.client_contract_number }}에 대한 보면 내가 장고 알고장고에 대한 장고 질문

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 

, 나는 클라이언트 계약 번호를 얻을. 하지만 특정 인보이스에 대해 알고 싶다고 가정 할 때 고객 이름을 어떻게 찾을 수 있습니까? 인보이스에 클라이언트의 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

당신은 관계를 따를 수 :

invoice.client_contract_number.client_number.name 

을 그건 그렇고, 당신의 필드 이름은 혼란이다. client_contract_number은 숫자가 아니며 계약서입니다. client_number도 숫자가 아니며 클라이언트입니다. client_contractclient으로 전화하면됩니다. 질문 업데이트

편집 난 당신이 여기에 뭘 하려는지 모르겠어요. invoices_list은 모든 인보이스의 쿼리 세트입니다. 클라이언트 이름이 이고 목록이 인 지 묻는 것은 당연합니다.

{% 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'라는 두 개의 필드가 있습니다. '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가 하나의 점임) 이상일 때마다 select_related를 사용하는 것이 좋습니다.

+0

기본적으로 하나의 열에 모든 송장 ID가 표시되는 테이블을 표시하려고합니다. 모든 열거 이름에 다른 열이 있어야합니다. 그러나 올바른 인보이스 ID는 동일한 행의 올바른 클라이언트 이름과 일치해야합니다. – Shehzad009

+0

@시 나는 당신이하려는 일을 이해합니다. 허용 된 대답이 정확합니다. 도트 구문을 사용하여 여러 관계에 걸쳐서 송장을 통해 클라이언트 이름을 가져와야합니다. 그것은 템플릿에서 이루어져야합니다. 너의 시야에서. 송장을 반복 할 때 송장 클라이언트 이름 속성을 추출해야합니다. select_related에 대한 나의 충고는 여전히 좋은 충고이다. 보기에서 select_related를 사용하고 템플리트에서 클라이언트 이름에 액세스하십시오. –