1
votes

I want to build web app where users can add different ads for sale(cars,apartments, gadgets etc...) and each of this categories models in django are specific and need specific fields in form for creating ad. Creating this forms using django is easy by using ModelForms.

DRF documentation says that A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields..... . So I guess I will create form on base of serializers.

My question is: How make a single React component which will render form on base of different serializers that frontend gets from Django-Rest-Framework?

Please any advice or links where I can read about this . If you need I can share some code . Thank you in advance.

1
I think you can only use the serializers in the backend code, to use as post, put, path requests and to return information as json. But not to render the react components, you need to write the form in the JSX code by yourself.grouchoboy

1 Answers

1
votes

Quite a late response, but here's some workaround.

You could check this npm package that convert JSON into React Form: https://www.npmjs.com/package/react-jsonschema-form

You could generate the JSON description from your Django model, by looping among attribute.

It's not straightforward but it worked fine for me.