Category,
verbose_name='Категория'
)
name = models.CharField(
'Имя',
max_length=150,
db_index=True
)
slug = models.SlugField(
'Название в URL',
max_length=200
)
articul = models.CharField(
'Артикул',
max_length=6,
unique=True
)
price = models.PositiveIntegerField(
'Цена'
)
stock = models.PositiveIntegerField(
'На складе',
default=0
)
discount = models.PositiveIntegerField(
'Скидка',
default=0,
help_text='Скидка указывается в процентах'
)
in_discount = models.BooleanField(
'Учавствует в распродаже ?',
default=False
)
brand = models.ForeignKey(
Brand,
null=True,
blank=True,
related_name='products',
verbose_name='Производитель'
)
__original_name = None
# Managers
objects = models.Manager()
with_image = WithImageManager()
def __init__(self, *args, **kwargs):
super(Product, self).__init__(*args, **kwargs)
self.__original_name = self.name
def get_absolute_url(self):
return reverse('products:detail', kwargs={'slug': self.slug, 'id': self.id})
def get_price(self):
return self.price - int(self.price * (self.discount / 100))
def save(self, *args, **kwargs):
sync = None
if kwargs.get('sync'):
sync = kwargs.pop('sync')
if not sync:
for fc in FilterCategory.objects.filter(categories=self.category):
pf = ProductFilter.objects.filter(filter_category=fc, product=self)
if not pf:
pf = ProductFilter(filter_category=fc, product=self)
pf.save()
# Overwrite Slug If Changed Name
if self.__original_name != self.name:
self.slug = slugify(unidecode(self.name))
super(Product, self).save(*args, **kwargs)
self.__original_name = self.name
def __str__(self):
return self.name
class Meta:
verbose_name = 'Товар'
verbose_name_plural = 'Товары'
ordering = ['price', '-id']
Ииии?
Обсуждают сегодня