do search with Django and JQuery

0

I'm trying to create a product search engine my model is this

class Producto(models.Model):
nombre = models.CharField(max_length=50, null=False, blank=False)
categoria = models.ForeignKey(Categoria, null=False, blank=False, on_delete=models.CASCADE)
precio = models.DecimalField(max_digits=5, decimal_places=2, null=False, blank=False)
precio_neto = models.DecimalField(max_digits=5, decimal_places=2, null=False, blank=False)
promo_inicio = models.DateField(null=True, blank=False)
promo_fin = models.DateField(null=True, blank=False)

and I have a template as follows I'm sorry but I did not find the way to put the html so I put the img.

well at the end of that file I generate this script, according to me using some jquery

In my view I have the next class in which a json returns me

class ProductoViewSet(ListView):
template_name = 'producto/index_admin.html'
model = Producto

def get(self, request, *args, **kwargs):
    query = request.GET['name']
    productos = Producto.objects.filter(nombre__contains=query)
    producto = serializers.serialize('json', productos, fields=('nombre', 'categoria'))
    print(producto)
    return HttpResponse(producto, content_type='application/json')

my url.py is this:

path('search/', ProductoViewSet.as_view(), name='search'),

The detail is that if you search the database and it returns the json successfully, but it does not load it to the table, but in a different format, see the images so they understand better.

How can I do that instead of showing the json in the browser I update it in the table that is already created?

    
asked by Rivas Kevin Live 06.05.2018 в 07:51
source

1 answer

0

If you want to use your view of how an API you can directly use the Django RestFramework library and you can save yourself a lot of work. If, on the other hand, you want to filter objects and design them, you can install django_filters which are forms dedicated to searching and are used just like the ModelForm.

    
answered by 07.05.2018 в 08:45