python - How to detect if a file has changed in a model save function -
when updating record existing file if file has changed need remove old file s3 bucket.
how in django can detect if file has changed?
this have tried (not tested) django have magic build in this?
def save(self, *args, **kwargs): existing_file = asset.objects.get(pk=self.pk) if existing_file != self.file: # remove s3 before saving new file
==
, !=
compare primary keys of model instances (look @ __eq__()
, __ne__()
methods implementation).
one way compare fields of model instances call model_to_dict() on both:
from django.forms.models import model_to_dict if model_to_dict(existing_file) != model_to_dict(self.file):
you can specify fields
, exclude
arguments control fields dump dictionary, in case, fields compare.
hope helps.
Comments
Post a Comment