Yuval Adam
Yuval Adam

Reputation: 165242

Render a TextField in admin as file upload widget

Is it possible to render a TextField in the Django admin, as a file upload widget and set the TextField's content to the content of said uploaded file?

Bonus points if it's possible to have both.

Upvotes: 2

Views: 576

Answers (1)

dan-klasson
dan-klasson

Reputation: 14190

A widget renders form elements. I would just use the Django Admin's save_model(). Something like this (untested):

# models.py
class Article(models.Model):
    text_file = models.FileField()

# admin.py
class ArticleAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        obj.text_file = request.FILES['text_file']
        obj.save()

Upvotes: 2

Related Questions