S
S
sazhyk2020-05-03 07:37:00
Django
sazhyk, 2020-05-03 07:37:00

How to choose the correct ratio of fields in this case?

I'm making a small corporate service using Django. Essence. There is some item . Available names of these products are created in the admin panel. Then, on the site, the user forms Applications from these goods. At the same time, in one application there can be only 1 product, but several pieces. Then the user can form an Order from Applications . At the same time, I must be sure that the same application will not fall into different orders.

Item Model
class Product(models.Model):
    title = models.CharField()
Application Model
class Order(models.Model):
    order_product = models.ForeignKey(Product)
    order_date = models.DateField( )
    number = models.IntegerField( )
Intended Order Model
class Proposal(models.Model):
    order_list = models.ManyToManyField(Order)    # Какое правильно выставить отношение?
But if there is a many-to-many relationship, then it will turn out that the same application can be "shove" into different orders. How to "combine" requests into orders?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-05-03
@sazhyk

in Order ForeignKey on Proposal

A
Ariurn, 2020-05-03
@Ariurn

If I understand correctly, you have an online store according to the scheme: you come, add goods to the basket and place an order. How I did it:

  • For each new session (django.contrib.sessions.models.Session) that is created when a user (even if not authorized) visits the site, an order (Order) is created, which has the status "Not executed" and is not displayed in the admin panel. It has a ForeignKey (which has on_delete=models.CASCADE) bound to the Session. For example:order.session=request.session
  • When adding a product to the "basket", an OrderProduct model is created, which has two ForeignKeys: one - Product, the second - Order, each has on_delete=models.CASCADE. They indicate which product was added to the cart and which user added it, respectively. Also, additional fields (for example, the quantity of the item added to the cart).
  • When placing an order, the status "Checked out" is set to it, it is displayed in the admin panel, and the administrator can work with it.

There is one caveat: with a large flow of users to the site, many orders (Order) will be created, which will not be deleted, because. Session are not automatically deleted (to my knowledge). Solution to the problem: run the command via cronpython manage.py clearsessions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question