Save . doc file in database with Django

Asked

Viewed 245 times

0

I’m developing an application with Djando and Python.

In models.py, to create an object cliente with a name, for example, I do the following:

class Cliente(models.Model):
    nome = models.CharField(max_length=255)

in forms.py I’ll tell you what:

class ClienteForm(forms.ModelForm):
    class Meta:
        fields = ('nome',)
        widgets = {'nome': forms.TextInput(attrs={'class': 'form-control'}),}
        labels = {'nome': _('Nome'),}

but how do I get this field instead of a text field to be a file .doc?

I need to attach a word document to each client, something like this:

class Cliente(models.Model):
    nome = models.CharField(max_length=255)
    documento = models.DOCUMENTO.DOC

1 answer

1

Django provides the type Binaryfield to save binaries, Voce can set as follows:

doc_file = models.BinaryField(blank=True)

But watch carefully for what the documentation says (Highlighted part at that link)

Although you might consider storing files in a database, keep in mind that this design is bad in 99% of cases. This type of field is not a substitute for proper handling of static files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.