Home > Net >  how to reset id whenever a row deleted in django?
how to reset id whenever a row deleted in django?

Time:12-12

i wanted to start id from 1 . whenever a row deleted it skips that number and jump to next number. enter image description here

like in this image it skips number from 1-6 and 8.

i want to set it as 1,2,3

this is my models.py

class dish(models.Model):
id = models.AutoField(primary_key=True)      
dish_id = models.AutoField
dish_name = models.CharField(max_length=255, blank=True, null=True)
dish_category = models.CharField(max_length=255, blank=True, null=True)
dish_size = models.CharField(max_length=7, blank=True, null=True)
dish_price = models.IntegerField(blank=True, null=True)
dish_description = models.CharField(max_length=255, blank=True, null=True)
# dish_image = models.ImageField(upload_to="images/", default=None, blank=True, null=True)
dish_image = models.ImageField(upload_to="media/", default=None, blank=True, null=True) #here added images as a foldername to upload to.
dish_date = models.DateField()

def __str__(self):
    return self.dish_name

this is views.py

def delete(request, id):
dishs = dish.objects.get(id=id)
dishs.delete()
return HttpResponseRedirect(reverse('check'))

CodePudding user response:

Deleting an object does not change any property of the remaining objects. You need to do that manually. First thing you need to do is to change the dish_id field:

# Change this:
# dish_id = models.AutoField
# to this:
dish_id = models.IntegerField(default=0)

Then in your views:

def delete(request, id):
    dishs = dish.objects.get(id=id)
    dishs.delete()

    all_dishes = dish.objects.all()
    for i, dish in enumerate(all_dishes):
        dish.dish_id = i   1
        dish.save()
    
    return HttpResponseRedirect(reverse('check'))

Separately, you really should write your classes in Pascal case, capitalizing the first letter of the class:

class Dish(models.Model):

And finally, Dish does not need an id field; that is created automatically, but you can leave that as is, it is not causing any problems.

CodePudding user response:

You can achieve those things using an overriding save() method like this

class dish(models.Model):
    id =models.AutoField(primary_key=True)      
    dish_id = models.BigIntegerField(unique = True)
    dish_name = models.CharField(max_length=255, blank=True, null=True)
    dish_category = models.CharField(max_length=255, blank=True, null=True)
    dish_size = models.CharField(max_length=7, blank=True, null=True)
    dish_price = models.IntegerField(blank=True, null=True)
    dish_description = models.CharField(max_length=255, blank=True, null=True)
    dish_image = models.ImageField(upload_to="images/", default=None, blank=True, null=True)
    dish_image = models.ImageField(upload_to="media/", default=None, blank=True, null=True) #here added images as a foldername to upload to.
    dish_date = models.DateField()
        
    def __str__(self):
        return self.dish_name

    def save(self, *args, **kwargs):
        count_obj = dish.objects.all().count() 1
        self.dish_id = count_obj
        super(dish, self).save(*args, **kwargs)


     
  • Related