Django and the Django Rest Framework: Best Practices

- Andrés Cruz

En español

Django and the Django Rest Framework: Best Practices

Django is one of those frameworks that have been on the market for a long time and that are used more and more together with the Python ecosystem every day: the creation of Rest APIs that has been, which as we have seen in various tutorials, videos and my courses, are easily scalable, Django is flexible and all this is thanks to the great modularity that the Python ecosystem has, which of course is also inherited from Django.

What is Django? What is it used for in the development of apps and Rest Apis?

Django is a set of tools known as a framework, which allows us to build all kinds of scalable web applications using a high-level programming language known as Python, among these apps that we can build are Rest APIs to connect to anything else, for For example, connecting an app in Django to Flutter.

The Rest APIs, although it is NOT a standard, is a measure that has been taken by multiple frameworks to build this type of system whose main purpose is to connect with other systems or applications as I mentioned before; so, for example, having an app in Django, we can connect it with apps created in Flutter, Android, iOS, React or anything else similar... which is excellent since with a single backend we can create multiple applications that consume this app in Django.

A real world example is an online shopping app, which is an app with a central repository created (for example) in Django and then you can create the apps for the client, either for mobiles and/or browsers using (for example) example) the technologies mentioned above...

The main concepts of Django and the REST framework are:

The model classes provide an object relational map (ORM) for the established database. A model is assigned to a table in the database. You can query the databases without launching a single line of SQL. With models, it's easy to define tables and relationships between them.

Views are in charge of the application process. They work as controllers in MVC, let's remember that Django uses MTV. You can implement them in many ways, such as functions or classes.

Serializer classes provide control of data types and request and response structures.

The templates or templates are files with static and dynamic content. They are made up of static code and other elements that depend on the context, although this is not the case in a Rest-type app.

Here are some best practices to follow when using Django for development:

Configuration Structure – Anyone who is new to development can easily create an application with ease using the Django framework. It's vital to create a structure that is easy to maintain and reuse when needed, whenever you use Django it generates a settings.py file that contains all the details related to the project or your framework so you don't make any changes to the settings.py file. main configuration and generate new files for different environments.

One project, multiple apps in Django: There can be many apps in a single Django project. An application is a collection of sets of related functions and models. It is good practice to maintain an order and define locations for standard functions. There is an application called api, by which you can define the routing of the other applications in the main folder of the application.

ViewSet: This is the most important component for viewing Rest APIs in Django models. Regular views in Django act as a controller for HTTP requests, viewsets give you options like create or list. The best feature of view sets is that they make your code consistent and you can avoid repetition; therefore, with this component, we can specify the structure and with which models they are going to work.

Generic Views: The Django framework has a feature called generic views that makes model-related functions and tasks easier. Some of the functions include retrieving data, creating and destroying lists. All you have to do is define the class. view as a child of the generic view.

Customizing functions: The generic views provided in the Django documentation list a variety of functions such as ListAPIView, CreateAPIView, RetreiveAPIVIew, RetrieveDestroyAPIView. But if you're looking to customize views, you can override get, post, put, delete, and other methods. You can also use mixins to override specific methods and functions like create, update, destroy, and others.

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 2d 19:22!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!