Question

I have my own text data file format to my Django application.

After uploading one file to Django through admin page, how can I show error to admin uploader if file contents is not in proper format?

Is there common way to handle this situation?

Était-ce utile?

La solution

You can use simple form validation, as everywhere in Django.

Pseudocode below

admin.py

class YourModelAdminForm(forms.ModelForm):
    def clean_your_field(self):
        if format_is_not_valid:
            raise forms.ValidationError('Format is not valid')


class YourModelAdmin(admin.ModelAdmin):
    form = YourModelAdminForm


admin.site.register(YourModel, YourModelAdmin)

or you can create your custom validator and use it on model field

models.py

class YourModel(models.Model):
    your_field = models.FileField(validators=[your_validator])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top