![]() ![]() we defined a custom get_queryset method in order to enable search and limiting contacts returned by the api to those belonging to the user making the request.we restricted access to authenticated users only, by setting the property "permission_classes" on our class.In our viewset, we already implemented a few features: To define our viewset it's required to specify which will be the serialization class (our ContactSerializer defined above) and what is the base recordset we will operate on, with the queryset property. This is a common pattern in django development and it's very powerful as it enables various levels of customization by leveraging python inheritance model. ![]() Our viewset is implemented by subclassing the ModelViewSet class provided by rest framework. get_queryset ( ) # Get only contact about current authenticated user serializers import ContactSerializerĬlass ContactViewSet (viewsets. We'll create a new file in our api folder called viewsets.py # api/viewsets.py from rest_framework import viewsetsįrom rest_framework. Now that we have a serializer, we can setup our endpoints. In our case, the minimal implementation is the following: # api/serializers.py from rest_framework import serializersĬlass ContactSerializer (serializers. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data." As explained in the rest framework docs: "Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes that can then be easily rendered into JSON, XML or other content types. To do so we need to define a "serializer". In other frameworks you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'. We'll implement the rest enpoints for our Contact model using the ModelViewSet, that will provide us a full set of endpoints to interact with the database.ĭjango REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet. Īdding rest framework to INSTALLED_APPS is not strictly required but it will enable the html rendering of APIs, wich will show an automatic interface to interact with your api in the browser, provided by rest framework itself. Let's install it pip install djangorestframeworkĪnd add it to the django INTALLED_APPS like we did with our api app: # addressbook/settings.py. We'll use the fantastic django rest framework package for this. Now that we defined our data model and enable the Django admin interface to edit them (in less than 20 lines of code and some management commands), let's implement a REST api that will be consumed by our React app. ![]() We can now spin up the python development server: python manage.py runserverĪnd point a browser to you should be able to login to the django admin interface with the user you created, and easily list and create contacts. The shell will ask for an username and password, choose what you want. ![]() Let' create an administrative account to access the "admin" interface python manage.py createsuperusrer Let's configure django admin to show our Contact model: # api/admin.py from django. To complete the data model related part, let's create a database with the django management commands (default is sqlite): python manage.py makemigrations That's it, our model definition is complete. To let the framework know about our model, add the api app to the INSTALLED_APPS list in django settings # addressbook/settings.py. Our simple Contact model has foreign key to the User model provided by Django, which we'll use to restrict visibility of contacts to the currently logged in user and to deny access to other users' contacts.Īs you can easily swipe the standard User model implementation for adding other fields and relations, we're using the get_user_model helper to make sure we're always referencing the correct model configured in Django. TextField (null = True, blank = True ) def _str_ (self ) : return "%s - %s" % (self. CharField (max_length = 20 )Įmail = models. User = get_user_model ( ) class Contact (models. Let's declare the model in the models.py file in the app we just created. Data Modelįor semplicity, we'll implement a simple data model that holds a contact information. The first step is setting up a dedicated python environment and a new django project, that we'll call addressbook: python -m venv env source env/bin/activateĭjango has the concept of apps, let's create a new api app cd addressbook-serverĭjango will create an api folder in our project. You can find all code of the django server in the addressbook-server folder in the example repo. We won't go in details of django setup here, please refer to the official docs. Let's create a new environment and a django project. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |