2👍
this is an excerpt taken from the docs:
Model.delete([using=DEFAULT_DB_ALIAS])
Issues a SQL DELETE for the object. This only deletes the object in the database; the Python instance will still exist and will still have data in its fields.For more details, including how to delete objects in bulk, see Deleting objects.
If you want customized deletion behavior, you can override the delete() method. See Overriding predefined model methods for more details.
I don’t get why you would nest a second loop like that inside the first one: for each object, delete all the objects then save the object?
cust = Customer.objects.all():
#delete all
for c in Customer.objects.all():
c.delete()
this is clearer, imho. I’ll try explain what SHOULD happen (not 100% sure, never tried something like that).
- cust is a QueryDict object and, as the docs say, it still populted
- your DB is now emptied of all the data related to the Customer Model
You should now be able to iterate through cust and save each object back again.
Mind the should, it’s very important
1👍
The cust
variable contains a python representation of informations about the n-th element stored in the QuerySet (obtained by Customer.objects.all()).
If you call the delete() method you hit the DB but all informations about your deleted object are still stored in the cust
variable. Thus, if you call after deleting the save() method on this, you store anew in the DB all informations that was contained in the variable.
- [Django]-Save 404 Error's URL in Django in models
- [Django]-Django AbstractBaseUser vs BaseUserManager when creating custom user
- [Django]-How to save each file in request.FILES with for loop in Django?
- [Django]-How do I upload Pillow processed image to S3 on Django?
- [Django]-Resource temporarily unavailable: mod_wsgi (pid=28433): Unable to connect to WSGI daemon process