Project: YugabyteDB (App Definition and Development / Database)
File: django.md

title: Django ORM headerTitle: Use an ORM linkTitle: Use an ORM description: Python Django ORM support for YugabyteDB menu: v2.18: identifier: django-orm parent: python-drivers weight: 700 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../sqlalchemy/" class="nav-link"> <i class="icon-postgres" aria-hidden="true"></i> SQLAlchemy ORM </a> </li> <li > <a href="../django/" class="nav-link active"> <i class="fa-brands fa-java" aria-hidden="true"></i> Django ORM </a> </li> </ul> is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Learn how to establish a connection to YugabyteDB database and begin basic CRUD operations using the steps in page. The following sections demonstrate how to perform common tasks required for Python application development using the Django ORM. To download the Django Rest Framework to your project, run the following command: ```sh pip3 install djangorestframework ``` Additionally, install the using the following command: ```sh pip3 install django-yugabytedb ``` After the dependencies are installed, start a Django project and create a new application using the following command: ```sh django-admin startproject yugabyteTest && cd yugabyteTest/ ``` Set up a new Django application using the following command: ```sh python manage.py startapp testdb ``` After creating the application, configure it to connect to the database. To do this, change the application settings to provide the database credentials. For better performance with YugabyteDB, use persistent connections (set `CONNMAXAGE`).In the file `yugabyteTest/settings.py` add the following code: ```python DATABASES = { 'default': { 'ENGINE': 'django_yugabytedb', 'NAME': 'yugabyte', 'HOST': 'localhost', 'PORT': 5433, 'USER': 'yugabyte', 'CONNMAXAGE': None, 'PASSWORD': 'yugabyte' } } ``` You need the application and rest framework in the INSTALLED_APPS field. Replace the existing code with the following: ```python INSTALLED_APPS = [ 'rest_framework', 'testdb.apps.TestdbConfig', 'django.contrib.contenttypes', 'django.contrib.auth', ] REST_FRAMEWORK = { 'DEFAULTAUTHENTICATIONCLASSES': [], 'DEFAULTPERMISSIONCLASSES': [], 'UNAUTHENTICATED_USER': None, } ``` The next step is to create a model for the table. The table name is `users` and contains four columns - `user_id`,`firstName`,`lastName`, and `email`. Add the following code to `testdb/models.py`: ```python rom django.db import models class Users(models.Model): userId = models.AutoField(dbcolumn='userid', primary_key=True, serialize=False) firstName = models.CharField(maxlength=50, dbcolumn='first_name') lastName = models.CharField(maxlength=50, dbcolumn='last_name') email = models.CharField(maxlength=100, dbcolumn='user_email') class Meta: db_table = "users" def str(self): return '%d %s %s %s' % (self.userId, self.firstName, self.lastName, self.email) ``` After creating the model, you need to create a

Serializers allow complex data such as querysets and model instances to be converted to native Python datatypes, which can then be rendered into JSON, XML, or other content types. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data. Copy the following code into `testdb\serializers.py`: ```python from rest_framework import serializers, status from testdb.models import Users from django.core.exceptions import ValidationError class UserSerializer(serializers.ModelSerializer): class Meta: model = Users fields = ('userId', 'firstName', 'lastName', 'email') ``` To finish all the elements of the application, create a ViewSet. In `testdb/views.py`, add the following code: ```python from django.shortcuts import render from testdb.models import Users from rest_framework import viewsets from testdb.serializers import UserSerializer class UserViewSet(viewsets.ModelViewSet): queryset = Users.objects.all() serializer_class = UserSerializer ``` Finally map the URLs in `yugabyteTest/urls.py` by adding the following code: ```python from django.urls import include, re_path from rest_framework import routers from testdb.views import UserViewSet router = routers.SimpleRouter(trailing_slash=False) router.register(r'users', UserViewSet) urlpatterns = [ re_path(r'^', include(router.urls)) ] ``` For Django versions earlier than 4.0, use the following code instead, as you can import the URLs using django.conf.urls: ```python from django.urls import path, include from django.conf.urls import url, include from rest_framework import routers from testdb.views import UserViewSet router = routers.SimpleRouter(trailing_slash=False) router.register(r'users', UserViewSet) urlpatterns = [ url(r'^', include(router.urls)) ] ``` This completes the configuration of your test application. The next steps are to create the migration files and apply the migrations to the database. To do this, run the following command: ```sh python3 manage.py makemigrations python3 manage.py migrate ``` A users table should be created in your database. Use the client shell to verify that the users table has been created. To run the application and insert a new row, execute the following steps. Run the Django project using the following command: ```shell python3 manage.py runserver 8080 ``` Insert a row using the following command: ```shell curl --data '{ "firstName" : "John", "lastName" : "Smith", "email" : "jsmith@yb.com" }' \ -v -X POST -H 'Content-Type:application/json' http://localhost:8080/users ``` Verify that the new row is inserted using the following command: ```sh curl http://localhost:8080/users ``` You should see the following output: ```output.json [{"userId":1,"firstName":"John","lastName":"Smith","email":"jsmith@yb.com"}] ``` You can also verify this using the client shell. Build Python applications using