선풍기
선풍기

Reputation: 961

Django Session counter

I'm making a food order project in Django, and I want to set unique order numbers for quick verification of the user when they come to pick the food up after the order.

If I create a random combination of numbers and letters, there can be an issue of two orders having the same order number. Is it possible to make the order number unique to each session?

Upvotes: 1

Views: 825

Answers (2)

Sidhin S Thomas
Sidhin S Thomas

Reputation: 885

Just use normal incremental order numbers and One Time Passwords(OTP)

User pyotp to generate random OTP for order.

When user orders, create a secret string. Then Generate an OTP for user and send it to them.

When they arrive to pick up, they show the OTP. You can check it using pyotp and the secret string to verify.

Use counter based OTP.

Store the secret key in the order model.

Simple and effective.

Upvotes: 1

Cory Madden
Cory Madden

Reputation: 5193

Are you storing this on the request.session or is it a model in the database? If it's the model in the database that's a lot simpler. Just set the unique=True kwarg in the field. Though you still have to generate the unique fields.

Look at this guy's _createHash function in his question. Combined with the accepted answer you can create unique order ID's this way. If it's just attached the the request.session object, it'll be more difficult(if not impossible) to be 100% sure that the string has not been repeated, because there's no way to query all the existing Sessions for a custom added attribute. I don't think session is the word you're looking for, though.

Upvotes: 0

Related Questions