....
brand = django_filters.ModelMultipleChoiceFilter(field_name='brand__name',
to_field_name='name',
label='Бренды',
queryset=Brand.objects.filter(is_active=True).exclude(
product_brand__isnull=True))
class ProductFilter(django_filters.FilterSet):
def __init__(self, *args, **kwargs):
self.slug = kwargs.get('slug', 0)
# self.slug = kwargs.pop('slug', False)
print(self.slug)
super(ProductFilter, self).__init__(*args, **kwargs)
self.filters['brand'].extra.update({
'queryset': Brand.objects.filter(is_active=True,
product_brand__category__slug=self.slug)
.exclude(product_brand__isnull=True),
'help_text': False
})
brand = django_filters.ModelMultipleChoiceFilter(field_name='brand__name', to_field_name='name',
label='Бренды')
class Meta:
model = Product
fields = ['brand']
__init__() got an unexpected keyword argument 'slug'
...
product_filtered_list = ProductFilter(self.request.GET, queryset=qs, slug=self.kwargs['slug'])
from . import async, base
^
SyntaxError: invalid syntax
# в файле "d:\development\django\parlamar.ru\env\lib\site-packages\celery\backends\redis.py" ругается на импорт
from . import async, base
@task()
def test():
print(1)
# shell
result = test.delay()
result.status
# settings.py
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/1'
CELERY_RESULTS_BACKEND = 'redis://127.0.0.1:6379/1'
# Category
class Category(Defualt, MPTTModel):
parent = TreeForeignKey('self', verbose_name='Категория', blank=True, null=True, related_name='children_category',
help_text='Выберите родительскую категорию', on_delete=models.CASCADE)
...
# Coupon
class Coupon(models.Model):
category = models.ForeignKey(Category, verbose_name='Категория товаров', on_delete=models.CASCADE)
code = models.CharField(verbose_name='Промокод', max_length=500)
valid_from = models.DateTimeField(verbose_name='Действителен от', blank=True, null=True)
valid_to = models.DateTimeField(verbose_name='Действителен до', blank=True, null=True)
discount = models.IntegerField(verbose_name='Скидка купона',
validators=[MinValueValidator(0), MaxValueValidator(100)], blank=True, null=True)
active = models.BooleanField(verbose_name='Активен', default=True)
....
# views при вводе купона
class CouponView(View):
def post(self, request):
form_coupon = CouponForm(request.POST)
if form_coupon.is_valid():
code = form_coupon.cleaned_data['code']
now = timezone.now()
try:
coupon = Coupon.objects.get(code__iexact=code,
valid_from__lte=now,
valid_to__gte=now,
active=True)
order = Order.objects.get(session_order=request.shop_session, is_history=False)
order.total_after_discount = order.total_cost_order - order.get_discount_money()
order.save()
except Coupon.DoesNotExist:
data = {
'title': 'Купона',
'msg': 'не существует!',
'error': 'error'
}
return JsonResponse(data)
# модель заказ
class Order(models.Model):
session_order = models.CharField(max_length=200, verbose_name='session')
coupon = models.ForeignKey(Coupon, verbose_name='Купон по заказу',
related_name='orders_coupon', null=True, blank=True, on_delete=models.CASCADE)
......
def get_coupon_discount(self):
total = sum(item.get_cost() for item in self.items.filter(category_product =self.coupon.category.id))
discount = round(total * (self.discount_percentage / 100))
return discount
# модель корзина
class OrderItem(models.Model):
order = models.ForeignKey(Order, verbose_name='заказ', related_name='items', on_delete=models.CASCADE)
product = models.ForeignKey(Product, verbose_name='Продукт', related_name='order_items', on_delete=models.CASCADE)
price = models.IntegerField(verbose_name='Цена', default=0)
quantity = models.PositiveIntegerField(verbose_name='Количество', default=0)
vol = models.FloatField(verbose_name='Объем', default=0)
weight = models.FloatField(verbose_name='Вес продукта', default=0)
category_product = models.IntegerField(verbose_name='Категория продукта', default=0)
def get_cost(self):
return round(self.price * self.quantity)