0
votes

explain me please how to use it in my Admin?

You can just create a custom ModelForm for your model, with the following:

remove_the_file = forms.BooleanField(required=False)

def save(self, *args, **kwargs):
    object = super(self.__class__, self).save(*args, **kwargs)
    if self.cleaned_data.get('remove_the_file'):
        object.the_file = ''
    return object

Use that form in your ModelAdmin, and there's no need to change the database.

there is what i created in forms.py:

class MediaForm(forms.ModelForm):

    remove_the_file = forms.BooleanField(required=False)

    def save(self, *args, **kwargs):
        object = super(self.__class__, self).save(*args, **kwargs)
        if self.cleaned_data.get('remove_the_file'):
            object.the_file = ''
        return object

And there is my admin.py:

class MediaAdmin(admin.ModelAdmin):
    raw_id_fields = ('parent',)

how should i change MediaAdmin class to apply it?

1

1 Answers

1
votes
class MediaAdmin(admin.ModelAdmin):
    raw_id_fields = ('parent',)
    form = MediaForm