0

I am new in Django. please help. I want to save image in BinaryField using form but it's not working. instead of upload in media folder, I want to save file Directly in Database using BinaryField.

Model.py:

class serviceDb(models.Model):
    Dev = 1
    QA = 2
    UAT = 3
    Production = 4
    environment_TYPES = (   (Dev, 'Dev'),   (QA, 'QA'), (UAT, 'UAT'),   (Production, 'Production'), )
    application = models.CharField(db_column='Application', max_length=255, blank=True, null=True)  # Field name made lowercase.
    startdate = models.DateField(null=True)
    expiredate = models.DateField(null=True)
    environment_type = models.PositiveSmallIntegerField(choices=environment_TYPES)
    CSR=models.BinaryField(editable=True)

Form.py:

class serviceForm(forms.ModelForm):
    app_attributes = {'oninvalid': 'this.setCustomValidity("Application field is required")', 'oninput': 'this.setCustomValidity("")'}
    startdate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    expiredate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    application = forms.CharField(widget=forms.TextInput(attrs=app_attributes))
    CSR = forms.FileField(required=False)
    class Meta:
        model = serviceDb
        fields = ('application', 'startdate', 'expiredate', 'environment_type','CSR' )

        error_messages = {
            'application': {
                'required': ("Application field is required"),
            },
            }
3
  • 2
    As per documentation Although you might think about storing files in the database, consider that it is bad design in 99% of the cases. This field is not a replacement for proper static files handling. Commented May 17, 2019 at 8:14
  • I guess you need to convert file to binary before sending it to form, maybe in views.py Commented May 17, 2019 at 8:14
  • @ruddra This is not I want to. I have to do it , so please help Commented May 17, 2019 at 8:27

1 Answer 1

1

The BinaryField expects BinaryData, so as @Vaibhav Vishal suggested you may need to convert it on your own.

I've never used the BinaryField so far and you should really consider not saving binary data in the database.

But in your case I suggest trying something like that

class ServiceCreateFormView(CreateView):
    template = ...
    form_class = serviceForm  # Should be `ServiceForm` btw.

    def form_valid(self, form):
        uploaded_file = form.files['CSR'].file  # I assume a `InMemoryUploadedFile` instance
        data = uploaded_file.file.read()

        # construct you own instance here using `data`
        self.object = ...

        return HttpResponseRedirect(self.get_success_url())

Please include more informations about your issue. Like a traceback, what exactly is not working, what did you try to resolve it?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.