Are Django forms ever used in Django Rest Framework? - django

The DRF tutorial includes the following line about DRF serializers vs Django forms:
The first thing we need to get started on our Web API is to provide a
way of serializing and deserializing the snippet instances into
representations such as json. We can do this by declaring serializers
that work very similar to Django's forms.
But from what I can see, it says nothing else about the relationship between forms and serializers.
Since DRF doesn't need the Forms' ability to render a model in a template, is it fair to assume that Forms have no purpose in DRF, and that serializers can handle all of the validation traditionally completed with forms?
If so, when I'm building an API, can I forget about templates and forms entirely?

Django REST Framework is used for creating REST API's which send XML or JSON.
Django Forms is used for creating HTML forms based on a given model.
As the role of an API generally doesn't involve sending HTML, Django forms would not be used.

Related

On what level in the django MTV architecture does the Django Rest Framework work?

I understand that django is an MTV architecture. I also understand that M is models, T is templates (which are views in MVC) and V is views (which are controllers in MVC).
I want to understand if the django serializers and views are views or templates according to the MTV when using Django Rest Framework.
There's no perfect 1:1 mapping there.
A DRF serializer is vaguely like a template in that it describes how the data should be serialized "onto the wire" when a DRF view outputs it, but unlike templates, serializers can also be used for data ingestion.
DRF views (and viewsets, which group views) map to MVC controllers or Django views in that they describe how the API endpoint works.
A DRF app doesn't necessarily need to work with Django models at all, but if it does, that bit is unchanged from how Django works.

Build react form based on serializer of Django Rest Framework

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.
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.

Serializers vs. Forms. How can I have both?

Salut friends,
I have been working with Django on a project for over a year now, and this project doesn't quite have forms (just a few). The way it is done is without Django Rest Framework, simple crispy forms, which are validated when the POST arrives at the corresponding view.
A month ago, I started to work on a different project, much bigger, that in the greatest part relies on forms. This project uses Django Rest Framework, serializers + viewsets, and renders forms through DRF serializers.
After attaining a certain understanding of all the validations that can be done with DRF serializers, I started to imagine it'd be a good idea to bring serializers and viewsets to my older project in order to enjoy the better structure offered by DRF.
Here is my question: Can (crispy) forms and serializers work well together? Is it better to render my forms with DRF if deciding to stick with DRF? I saw a couple of people who use both, but it is unclear to me at this point. Is there a more standard way to do so, which is used by a majority?
Thanks!
I use DRF with Crispy Forms, I started doing so from the start of using DRF because they recommended it in their documentation...
Requirements REST framework requires the following:
Python (2.7, 3.2, 3.3, 3.4, 3.5)
Django (1.8, 1.9, 1.10)
The following packages are optional:
coreapi (1.32.0+) - Schema generation support.
Markdown (2.1.0+) - Markdown support for the browsable API.
django-filter (0.9.2+) Filtering support.
django-crispy-forms - Improved HTML display for filtering.
django-guardian (1.1.1+) - Object level permissions support

how to use django restframework as a backend for mobile apps

I need to built an "API". Using django restframework. API has to support multiple platforms like mobile apps, webapps.
API will be used as a backend which will store all information. But my problem is how do I access users information using API. I mean normally django has user model. And we access user related stuff using request.user. But how do I access request.user information using API. Please pardon me for asking such question. But as I am new to developing API for Mobile apps. I am facing difficulty.
I think the easiest way to think about it, is that Django Rest Framework will (normally) return or process JSON data, rather than an HTML page / HTML form data.
Your models stay the same.
If you use Django's ModelForms then DRF's ModelSerialzers are very similar in use.
Likewise, using Django's class based generic views, are very similar to DRF's generic views are very similar - except rather than processing POST data from an HTML forms, they will receive JSON data. The generic views cover the same things - create via POST, update via PUT, delete via DELETE.
Like I say the main difference is that you will be dealing with JSON in place of HTML.
(You could easily use bog standard Django views without the rest-framework and return or process JSON. DRF takes a fair bit of the boilerplate code out of the process).

Dealing with Django model forms + REST Framework with Angularjs Single Page Application

I'm trying to implement a SPA in my existing Django project with Angularjs. I have setup the start of a API using Django Rest Framework to retrieve my data in angular.
Some of my django model forms have a lot of complexity in them and would love to somehow pass it to the client side.
I could be wrong here but since I'm setting this project up as a SPA, once I send the user to the project from my django urls.py, then the angular routing via $routeProvider take over and talk to all my angular stuff to get data (controllers, services, etc.,) via the REST api. This then prevents me from sending my form over via context.
Every example I've seen with angularjs and django forms always explicitly writes out each field in the form. Is there anyway for me to get my django model form that has a bunch of code in its __init__ method, over to the client side without having to set each field manually?
I've never tried this, but here's how I'd start on trying to do it.
You can use the in-built as_p(), as_ul() and as_table() methods of the form to pass the form HTML from the REST API, e.g.
class MyApiView(ApiView):
def get(self, request, format=None):
form = MyForm()
response['form_html'] = my_form.as_ul()
return Response(response)
You would then need to build the form skeleton and submit buttons in Angular, then insert form_html as the form body just as you would do in a standard django template.
Disclaimer: It's been a while since I've properly used django-rest-framework so the syntax in my example mught not be correct, but the basic idea is there