2013-06-15 1 views
0

저는 일대일 관계가있는 두 테이블에서 데이터를 추출하는 우아하고 효율적인 방법을 찾고 있습니다. ,Django queryset 외래 키의 모든 필드

이름, contact_phone, contact_fax :

class Contact(models.Model): 
    name = models.CharField(max_length=100) 
    country = models.CharField(max_length=100) 
    status = models.BooleanField() 

class ContactDetails(models.Model): 
    contact_name = models.ForeignKey(Contact) 
    contact_phone = models.CharField(max_length=100) 
    contact_fax = models.CharField(max_length=100) 

내보기 :

def showContact(request): 
    contacts = ContactDetails.objects.select_related('name').all() 
    print contacts.values() // debugging in console 
    return render(request, 'contacts/listContacts.html', { 'contacts': contacts }) 

무엇을 달성하려고하는 것은 같은 내 템플릿의 목록입니다

여기 내 모델입니다 국가, 지위

이것은 다시 매우 단순해야합니다. st 지금까지 이걸로 잠시.

감사합니다.

+2

'.contact_name.country'와'.contact_name.status'를 사용하는 것이 잘못된 이유는 무엇입니까? –

+0

그게 전부입니다. 내가 생각하기에, 슈퍼 간단합니다. 고마워요! – David

답변