Friday 11 June 2010

Clearing FileFields in Django's admin

This post is as much a reminder for me as it will be helpful to others. Yesterday I added a couple of FileFields to a Django model, the point of them is to allow overriding of some site images. So the idea is that you can upload images or swf files to these fields, they'll replace the ones on the site, until you are bored of them then you can delete them and the original images will be restored.

After adding the FileFields to the model, they nicely appeared in the Django admin site. I uploaded an image, the override worked perfectly, now to restore the original image...

And that's where you hit a problem. There is no way in the Django admin to clear a FileField, you can replace it, but you can't clear it to NULL. Happily there is a workaround[1]. You first need to create a custom ModelForm for your model which includes an extra checkbox like so:

class MyCustomForm(ModelForm):
    clear_the_image = forms.BooleanField("Clear the image", required=False)

    class Meta:
        model = MyModel

    def save(self, commit=True):
        model_instance = super(MyCustomForm, self).save(commit=False)
        if self.cleaned_data.get('clear_the_image'):
            model_instance.the_file_field = None

        if commit:
            model_instance.save()

        return model_instance


Then in your MyModelAdmin class:

class MyModelAdmin(ModelAdmin):
    form = MyCustomForm


That's it. When you save the form, it will see if you asked to clear the image, if so it will set it to None and then save. Job done.

[1] Adapted from this: http://www.developerit.com/2010/04/17/django-admin-add-a-remove-file-field-for-image-or-filefields

5 comments:

  1. Internet has many advantages for their users, Internet is always available for their users, it has vast range of information for their users, Internet provides many ways to able us to keep in contact with the help of writing services australia people at all hours of the day and night, we can easily contact with people through Internet.

    ReplyDelete
  2. Hi! the topic is very much in demand today and everyone wants to read about it. Thanks assignment writing services reviews

    ReplyDelete
  3. Create a file storage backend that saves the binary data, together with the file name and type, in a BinaryField in the database as an alternative. This allows you to stay within the FileField paradigm while yet having access to metadata if needed. If you had to implement it yourself, it would be a lot more effort, but it is already done in the form of db file storage. Because upload.read() was not saving the image in the correct encoding in Postrgres and the image could not be presented in an HTML template, I actually followed @Ania's solution with a workaround. Visit our game link: https://lokicraft.io/ and read more about the game.

    ReplyDelete
  4. Homework is how teachers check their students’ knowledge across a subject. Although oral answers and participation during seminars and lectures are important https://essaysrescue.com/slader-review/, nearly 65% of a course score will depend on writing homework - essays, research papers, coursework, assignments, and a thesis paper, to name a few.

    ReplyDelete
  5. For others, it is considered very strange that I do nothing, but there are ready-made assignments, this is because I have a freepaperwriter, thanks to which you can easily earn good grades from those writers who are there.

    ReplyDelete