Passing Django form data to frondend framework (DRF)

Viewed 66

Background

I ran into a really annoying problem here. I've decided to use Svlete as my frontend. Also, I have a lot of endpoints in my REST api that expect form data. In the past I would just forms.py in my app's directory and then add a form like:

class ProductsModelForm(forms.ModelForm):
    class Meta:
        model = Product
        fields = (
            'name',
            'weight',
        )

class ProductForm(forms.Form):
    name = forms.CharField()
    weight = forms.IntegerField()

And would then use

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Submit</button>
</form>

In the HTML template. All cool.

DRF + Frontend Framework

Obviously I still have the form in the forms.py but now I have to define the form in the fronend too. And with bigger forms it gets really annoying because I always have to compare that I have every field in my frontend form that I have defined in the django backend form. Is ther any solution to this?

1 Answers

Use DRF SERIALIZERS.

DRF SERIALIZERS ARE JUST LIKE FORMS; THEY VALIDATE AND CLEAN DATA.

Forms are best for monolithic sites.

Related