Home > database >  Problem when deleting ImageField from database
Problem when deleting ImageField from database

Time:07-20

models.py:

Picture = models.ImageField('Picture', null=True, blank=True)

When I give the command

person.Picture.delete()

the picture is deleted from the directory, but in the database still has the record of the file path in Picture as shown in image below. How can I delete it?

enter image description here

CodePudding user response:

Set the field to None as well, so:

person.Picture.delete()
person.Picture = None
person.save()

Note: normally the name of the fields in a Django model are written in snake_case, not PascalCase, so it should be: picture instead of Picture.

CodePudding user response:

You need to try

person = Person.objects.get(User=User_) 
person.Picture.delete(save=False)
...
person.save()

Try setting the path in your model something like below

Picture = models.ImageField(upload_to='Picture', null=True, blank=True)

CodePudding user response:

Try this person.Picture.delete(save=False)

https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.fields.files.FieldFile.delete

  • Related