Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent the deletion of old data when updating only new information? #111

Open
rinshankolayil opened this issue Nov 20, 2024 · 1 comment

Comments

@rinshankolayil
Copy link

How can I make sure the file doesn't get deleted when I'm only updating its information and not the file itself?

form_data = {
    'name': name,
    'category': category,
    'description': '',
    'reference_number': reference,
    'is_active': is_active,
    'expiry_date': expiry_date,
}
form_file = {}
if media_path:  # Check if the file is posted
    form_file['media_path'] = media_path

if file_id:
    file_instance = get_object_or_404(ModelName, id=file_id)
    if not media_path:
        form_file['media_path'] = file_instance.media_path
    form = FormName(
        form_data, form_file, instance=file_instance)
else:
    form = FormName(form_data, form_file)

if form.is_valid():
    file_instance = form.save()

@vinnyrose
Copy link
Collaborator

If you update the file field and the string value is different than it was before it will trigger the deletion. Ensure the value from the instance matches the value in the form to avoid the deletion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants