A
A
Alexander2017-06-05 10:59:51
Django
Alexander, 2017-06-05 10:59:51

How to automatically assign a number to an "Order"?

Good afternoon.
There is a model:

class Order(models.Model):
    '''Заказ'''
    order_number = ???
    date_added = models.DateTimeField(auto_now_add=True)
    date_shipment = models.DateField()

    def __str__(self):
        return self.order_number

Tell me how to automatically assign a unique value to "order_number". It is necessary that at the time of creating the order, the number is immediately assigned and reserved, even if the client does not save the order.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2017-06-05
@deliro

What id does not suit?

J
JonGalt, 2017-06-05
@JonGalt

Well, for example, do this in the view:

...
next_number = Order.objects.count() + 1
form = OrderForm({'оrder_number': next_number})
...

depending on what your uniqueness should be based on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question