0

이것은 내 모델 및 뷰입니다. 나는 각 제품마다 30 % + 5 %, 30 % + 10 % 및 20 % + 5 %의 고정 할인을받는 3 가지 유형의 사용자가 있습니다. 내가 열심히 주어진 사용자에 대한 할인 된 가격을 반환 Ancillary에 메서드를 추가 모델사용자 유형에 따라 쿼리 데이터를 변경하십시오.

from django.db import models 
from django.core.urlresolvers import reverse 

class Ancillary(models.Model): 
    product_code = models.CharField(max_length=60, null=True) 
    type = models.CharField(max_length=120, null=True) 
    product = models.CharField(max_length=120, null=True) 
    standard = models.CharField(max_length=120, null=True) 
    measurement = models.CharField(max_length=120, null=True) 
    brand = models.CharField(max_length=120, null=True) 
    price = models.Int(max_length=120, null=True) 

class Meta: 
     verbose_name_plural = "Ancillaries" 
def get_absolute_url(self): 
     return reverse('ancillaries') 
def __unicode__(self): 
     return u'%s %s %s %s %s %s %s' % (self.id, self.product_code, self.type, 
          self.product, self.standard, 
          self.measurement, self.brand) 


class AncillaryDetail(DetailView): 
    model = Ancillary 
    def get_context_data(self, **kwargs): 

     context = super(AncillaryDetail, self).get_context_data(**kwargs) 
     context['Ancillary_list'] = Ancillary.objects.all() 
     return context 

답변

0

에서 각 사용자에 대한 가격을 코딩없이 그것을 할 수있는 방법이있다. 그러면보기에서 호출하여 로그인 한 User을 전달할 수 있습니다.

class Ancillary(models.Model): 
    ... 
    def get_discounted_price(self, user): 
     # check type of user and return discounted price 

이러한 다양한 유형의 사용자를 어떻게 정의 할 지 말씀하지 않으 셨습니다. 가장 일반적인 해결책은 User 개체를 사용자 지정 (또는 User 프로필 사용)하여 유형을 가리키는 것입니다. 유형은 관련 할인을 제공하는 다른 모델입니다. 그런 다음 아무 것도 하드 코드하지 않고 관리자로부터 유형 및 할인 금액을 제어 할 수 있습니다.

+0

은 많은 관계가있는 것입니까? – vvdect

+0

@wdect : 사용자가 여러 유형을 가질 수 있는지 여부에 달려 있습니다. 당신이 말했던 것에서 나는 가정하지 않습니다. 어떤 경우에 그것은 단지 "ForeignKey"일 것입니다. –

관련 문제