django
  • templates
  • modeling
  • 2010-06-04 7 views 0 likes 
    0

    나는 "를 통해"사용 ManyToMany 관계를 사용하여 내 객체를 개조 : link text장고 복잡한 모델과 템플릿

    class Receipt(models.Model): 
        name = models.CharField(max_length=128) 
        (...) 
        components = models.ManyToManyField(Product, through='ReceiptComponent') 
        class Admin: 
        pass 
    
        def __unicode__(self): 
        return self.name 
    
        def url(self): 
        return self.id 
    
    class ReceiptComponent(models.Model): 
        product = models.ForeignKey(Product) 
        receipt = models.ForeignKey(Receipt) 
        quantity = models.FloatField(max_length=9) 
        unit = models.ForeignKey(Unit) 
        class Admin: 
        pass 
        def __unicode__(self): 
        return unicode(self.quantity!=0 and self.quantity or '') + ' ' + unicode(self.unit) + ' ' + self.product.genitive 
    

    이 좋아 보인다,하지만 난 그것으로이 문제가 :

    1) 관리자 관리 패널에 영수증과 쉽게 연결되어 있지 않음 = 새 구성 요소를 추가해야하는 경우 - 구성 요소로 이동하여 영수증에 연결되는 구성 요소를 만들어야합니다 - 어쩌면 유일한 해결책 일 수 있습니다. 그러나 영수증에 더 직관적입니다.

    2) 템플릿을 사용하여 인쇄 할 수 없습니다 :

    views.py :

    (...) 
    def detail(request, receipt_id): 
        receipt = get_object_or_404(Receipt, pk=receipt_id) 
        components = receipt.components.all() 
        return render_to_response('receipt.html',{'receipt' : receipt, 'components' : components,} 
    (...) 
    

    가 receipt.html :

    <h1>{{ receipt.name }}</h1> 
    {% for component in components.all %} 
    <div class='component'>{{ component }}</div> 
    {% endfor %} 
    

    답변

    1

    .all로 수행 한 작업은 정확히 내가 의미했던 것입니다. 처음에는 두 곳, .all()보기 및 .all 서식 파일에있었습니다.

    '오류'의 이유는 분명합니다. 구성 요소는 m2m 필드가 제품입니다. 이것이 당신이 작성한 코드입니다. 이 구성 요소는 중간 모델 ReceiptComponent이 아닌 제품 세트입니다.

    UPD은 : 단순히 귀하의 모델을 떠나, 그리고 Receipt

    +0

    바로! 구성 요소 = receipt.receiptcomponent_set.all() 는 문제를 해결합니다. Python 및 Django가 나를 놀라게하지 않습니다; D –

    0

    1) 당신이 inlines를 시도?

    2) 템플릿에서 .all을 제거하면 필요하지 않습니다. 수도 있습니다 구성 요소 = 목록 (receipt.components.all())

    0

    1) 완벽하게 보입니다! .ALL 예외의 원인 제거 :

    Caught an exception while rendering: 'ManyRelatedManager' object is not iterable 
    

    를하지만 좋은 품질의 코드를 내가 코드 템플릿에서 이동해야 이해 :

    ; (D

    2 인라인 = 인라인! 다른 사용자 힌트)

    views.py :

    def detail(request, receipt_id): 
        receipt = get_object_or_404(Receipt, pk=receipt_id) 
        components = receipt.components.all() 
        additionals = receipt.additionals.all() 
    
        return render_to_response('drinkbook/receipts/receipt.html',{'receipt' : receipt, 'components' : components, 'additionals' : additionals, }) 
    

    템플릿 :

    h1>{{ receipt.name }}</h1> 
    {% for component in components %} 
    <div class='component'>{{ component }}</div> 
    {% endfor %} 
    {% if receipt.additionals %}Ponadto: 
    {% for additional in additionals %} 
    <div class='additional'>{{ additional }}</div> 
    {% endfor %} 
    {% endif %} 
    <p>{{ receipt.desc|safe }}</p> 
    

    확인. 이제는 작동하지만 구성 요소의 결과는 Product입니다. 유니 코드은 ReceiptComponent가 아닙니다. 유니 코드 (제품의 하위 항목). 왜?

    +0

    이상에서 receiptcomponent_set를 사용합니다. 하나의 대답은 사라집니다 ... –

    +0

    ... D 답이 올라갑니다; D –

    관련 문제