M
M
mikeydown2020-06-02 17:28:04
Django
mikeydown, 2020-06-02 17:28:04

How to convert ORM query to pure SQL query in Django?

I am newbie. I am doing a study project on Django (online store), the requirements indicate that you cannot use ORM. I have an ORM query that, when placing an order for each item in the cart, assigns its attributes to the OrderItem entity (position in the order). I can't figure out how to pass these product parameters in the cart in raw SQL query. Actually, the ORM request itself looks like this:

for item in cart:
                OrderItem.objects.create(order=order,
                                         record=item['record'],
                                         price=item['price'],
                                         quantity=item['quantity'])

Context:
View to create an order:
def order_create(request):
    cart = Cart(request)
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(order=order,
                                         record=item['record'],
                                         price=item['price'],
                                         quantity=item['quantity'])
            # очистка корзины
            cart.clear()
            return render(request, 'orders/order/created.html',
                          {'order': order})
    else:
        form = OrderCreateForm
    return render(request, 'orders/order/create.html',
                  {'cart': cart, 'form': form})

Help, please, to write raw SQL request. If you need any more parts of the code - I will add.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-06-02
@bacon

I didn’t see the requirements more stupidly, with my hands like this https://docs.djangoproject.com/en/3.0/topics/db/sq... and with django_debug_toolbar or just a logger with django.db.backends will show all requests

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question