Issue
I'm new in django, I want to create custom Cart number that starts with #. When a new record comes into database. #1-1000, #1-1001, #1-9999, .... #2-1000, ...#2-9999 etc.
This is my model
class Cart(models.Model):
# id = models.CharField(primary_key=True, editable=False, max_length=10)
user = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name="cart")
create_date = models.DateField(auto_now_add=True)
cart_number = models.CharField(max_length=500, default=increment_cart_number, null=True, blank=True)
total_summa = models.FloatField()
time = models.TimeField(auto_now_add=True)
The cart_number
will be always in range 1000- 9999 so how can I do that with increment_cart_number
function?
Solution
def increment_cart_number():
last_cart_number = Cart.objects.last()
if last_cart_number.exists():
last_num = last_cart_number.cart_number.replace('#','').split('-')
if int(last_num[1]) >= 9999:
first_number = int(last_num[0]) + 1
secound_number = 1000
result = '#' +str(first_number)+ '-' +str(secound_number)
return result
elif int(last_num[1]) < 9999:
secound_number = int(last_num[1]) + 1
result = '#'+ last_num[0]+ '-' +str(secound_number)
return result
else:
return '#1-1000'
But I suggest to you for more cleaner way and best practice to do it in another way:
Make this field as AutoField
and make defalut value 1000 to start from it.
cart_number = models.AutoField(auto_created = True, default=1000)
After that when you want to print this value add '#' to it from frontend that will be better and faster and easier.
Answered By - Hashem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.