简体   繁体   中英

How to use a queryset result to filter other queryset in DJango ORM?

I am trying to call a view using AJAX, but I have a problem. I have a token for the submit, the function that calls to Django view is working, I followed all the instructions of this link: https://realpython.com/blog/python/django-and-ajax-form-submissions/ , but in the console I get the following error:

500: DoesNotExist at /Buscar/Producto/
InventarioProducto matching query does not exist.

/Buscar/Producto/ is the URL connected to the view, that is working, I think that is not the problem.

After importing the models, I tried the following in Django shell:

resp_producto=Producto.objects.filter(codigo_producto=9786071411532)  
resp_inventario=InventarioProducto.objects.get(producto_codigo_producto__in=resp_producto)
resp_precio=Precio.objects.filter(producto_codigo_producto__in=resp_producto,estado_precio='1').order_by('-idprecio')[:1]

In the shell, if I print the variables were I saved the querysets, I can see the results, so i don't know why this is not working on the view.

9786071411532 is a product code that exist in the MySQL database, it is saved in a column named codigo_producto, which is the same name of a field saved in the model Producto, actually it is a primary key.

Explaining the models: InventarioProducto has a field that is a foreigin key from Producto. The field in the model InventarioProducto is called producto_codigo_producto, and the primary key of Producto is called codigo_producto. So producto_codigo_producto referes to codigo_producto. The model Precio has the same foreign key with the same name used in the model InventarioProducto, so they work in the same way.

Also I make sure that all the data that I'm requesting really exists.

Here is the view:

def BuscarProducto(request):
    if request.method == 'POST':
        txt_codigo_producto = request.POST.get('id_codigo_producto')
        response_data = {}
        resp_producto=Producto.objects.filter(codigo_producto=txt_codigo_producto)
        resp_inventario=InventarioProducto.objects.get(producto_codigo_producto__in=resp_producto)
        resp_precio=Precio.objects.filter(producto_codigo_producto__in=resp_producto,estado_precio='1').order_by('-idprecio')[:1]
        response_data['result'] = 'Create post successful!'
        response_data['codigoproducto'] = resp_producto.codigoproducto
        response_data['lote'] = resp_inventario.idinventario_producto
        response_data['descripcion_producto'] = resp_producto.descripcion_producto
        response_data['precio'] = resp_precio.valor_precio

        return HttpResponse(
            json.dumps(response_data),
            content_type="application/json"
        )
    else:
        return HttpResponse(
            json.dumps({"nothing to see": "this isn't happening"}),
            content_type="application/json"
        )

I've moved the SOLVED text to an answer:

I changed this:

resp_inventario=InventarioProducto.objects.get(producto_codigo_producto__in=resp_producto)

To this:

resp_inventario=InventarioProducto.objects.filter(producto_codigo_producto__in=resp_producto)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM