M
M
mspuz2022-01-04 23:40:50
Django
mspuz, 2022-01-04 23:40:50

Django 4 - how to delete a file when deleted from admin?

Hello.

1. When adding a file for a model in the admin panel, it is possible to check the box and, when resaving, delete this file from the database. But how to delete this file physically?

2. There are many examples on the Internet on how to delete a file when deleting the model itself. And how to delete all files, if there are several.

Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2022-01-04
@deliro

What does "how" mean? Answer the question: how to get all the files associated with the model? Now turn it into a model method

def get_all_my_files(self) -> list[str]:
    result = []
    result.extend(get_from_here())
    result.extend(and_from_there())
    return result

And hang on pre_delete a signal to receive files, and then delete

M
mspuz, 2022-01-05
@mspuz

Thank you. Slightly confused and blunted. But I solved everything a little easier - I use django_cleanup.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question