Created
January 12, 2022 11:55
-
-
Save suzzukin/0f170c07b0d2a904aca8924dd0595566 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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