Skip to content

Instantly share code, notes, and snippets.

@suzzukin
Created January 12, 2022 11:55
Show Gist options
  • Save suzzukin/0f170c07b0d2a904aca8924dd0595566 to your computer and use it in GitHub Desktop.
Save suzzukin/0f170c07b0d2a904aca8924dd0595566 to your computer and use it in GitHub Desktop.
from django.core import serializers
from django.htttp import JsonResponse
from django.db import models
class Product(models.Model):
categories = models.ManyToManyField(Category,
related_name='products',
blank=True, verbose_name=u"категории")
related_products = models.ManyToManyField('Product',
blank=True,
verbose_name="связанные продукты")
sku = models.CharField(u'артикул', max_length=128, validators=[validators.check_bad_symbols], unique=True)
price = models.DecimalField(u'цена', max_digits=12, decimal_places=4)
slug = models.SlugField(u'slug', max_length=80, db_index=True, unique=True)
name = models.CharField(u'название', max_length=128)
title = models.CharField(u'заголовок страницы (<title>)', max_length=256, blank=True)
description = models.TextField(u'описание', blank=True)
def live_search(request):
q = request.GET.get("q", "")
if q:
results = Product.objects.filter(
Q(sku__contains=q)
|
Q(name__contains=q)
|
Q(description__contains=q),
)
results = serializers.serialize('json', data)
return JsonResponse({'data': data})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment