0
這是我的模型和視圖。我有3種類型的用戶,每種產品的固定折扣分別爲30%+ 5%,30%+ 10%和20%+ 5%。有沒有辦法爲我做沒有硬編碼爲每個用戶的價格在模型根據用戶類型更改查詢數據
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
是否與多對多的關係? – vvdect
@wdect:這取決於用戶是否可以有多種類型。從你所說的我不認爲,在這種情況下,它只是一個'ForeignKey'。 –