0

I have a Django project which in i use file field to upload files.

What i want to do is to delete the file field's file which is linked to the object im trying to delete.

I used This document as an example: https://djangosnippets.org/snippets/10638/

when i use these functions i get the dollowing error on the server: "Error 32 - The process cannot access the file because it is being used by another process" I think that the process which uses it is the django server because i tried to delete it with a regular python script (same functions) and with batch and it worked just fine.

Anyone knows a way around this?

1 Answer 1

3

You want to delete files (FileField, ImageField) when whole model is deleted ?

Try like this.

from django.db import models

class MyModel(models.Model)
    ''' for example this is your model '''
    name = models.CharField(max_length=255)
    image = models.ImageField(upload_to='/test/var/')

# this is your view
mm = MyModel.objects.get(pk=111)
# print(mm.image)
mm.image.close() # add this line
mm.image.delete() # this will delete file and not model
Sign up to request clarification or add additional context in comments.

5 Comments

no, i want to delete the file which the object is linked to**
I updated my answer, check it. Does not matter . FileField is super class of ImageField.
I get the same error: Error 32 - The process cannot access the file because it is being used by another process
See my last update. Restart server also. Hope this fix your problem.
I don't know why this answer was so hard for me to find but I'm glad I found it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.