M
M
maniacus262020-01-29 15:49:53
Django
maniacus26, 2020-01-29 15:49:53

Django get filename from mode with FileField?

Good afternoon !
There is a model that contains, in addition to text fields, a file.

class Inform_note(models.Model):
    id = models.AutoField(primary_key=True)
    in_note_on = models.FileField(upload_to='notes', blank=True)
    ...


In forms it is possible to create records, add and modify files.
Now there are 10 such entries.

After creating a form with a file (CreateView) or updating it (UpdateView), I want to get the file name or the path to the attached file for its further processing.

view.py

class Inform_noteEditView(generic.UpdateView):
    model = Inform_note
    fields = '__all__'
    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.helper = FormHelper()
        form.helper.add_input(Submit('submit', 'Create', css_class='btn-primary'))
        form.fields['in_note_on'].label = "Приложенный файл"


How to take filename from form.fields['in_note_on'] ?
Or how to do it better?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maniacus26, 2020-02-03
@maniacus26

Solution:
Added to view.py before function def get_form(self, form_class=None):

queryset = Inform_note.objects.all()
    def get_object(self):
        ob = super().get_object()
        print(ob.in_note_on)

Thus, it turned out to correctly refer to the attached file.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question