Django API Scaffold Part II

Now that your environment is setup, time to write some code!

For part one of setting up a Django API, please head over here:

The first part of setting up your API is setting up your classes within the models.py file. One of the coolest parts about Django Restframework is using the built in Auth model (a User Model). To use this, first run the following command:

There is also a djangorestframework-jwt package that is pretty similar, but I have preferred using the simple auth package. To use this package, you will also need to add the following to the bottom of your settings.py file:

Now that your Auth package is in your environment, it’s really time to make some models. These will be your classes for your database. The below shows a many:many relationship, with Users and Homes being connected through Favorites:

Image for post
Image for post

As you can see, the User class is defined first. This will allow you to extend Django’s built in Auth. The User class comes predefined with some fields already (username, password, email, first name & last name), so there is no need to define these fields again in the User model. Additionally, the User class extends the , while the other models extend .

Each field is defined as being either a . Additional field types can be used too, such as Booleans or Dropdown list options. Line 40 of the above code is very powerful, in that it is able to connect Homes to Users (similar to from ActiveRecord with Rails). Unfortunately, this line of code cannot be used in the User model, as the Home model that it would connect to hasn’t been defined yet. Alternatively, you could import models, but I have enjoyed having all my models in one single file. You will be able to define relationships through your other classes within your serializers (see below). After your models are setup, run:

and

It’s critical to define the User class before making your first migration. This will allow you to bring a ton of authentication functionality into your app. After your first migration, you can now create an admin user for your app, with:

Follow the prompts to define the user with a username (generally people have used ‘admin’ for the superuser), along with a email and password. Next, create a serializers.py file and you can setup your serializers file like so:

The serializers define how your API will be structured and which fields will display at each endpoint. This one of the big differences between Django and Rails. With Rails, a lot of the API structure comes out of the box. It’s a little tricky to connect relationships in Django (or certainly more work than with Rails). This is why I setup three ObjectSerializers first, and then you will later be able to reference those serializers within each endpoint’s actual serializer. The create method within the User class helps extend the built-in Auth we previously setup in the models.

Next, define your views.py like so:

Next, you’ll also need to create a urls.py file, and register your endpoints like the following:

Additionally with the url endpoints, you’ll need to register them in the admin.py file:

from django.contrib import adminfrom .models import User, Home, Favorite# Register your models here.admin.site.register(User)admin.site.register(Home)admin.site.register(Favorite)

The final step, is setting up the urls.py in your project folder:

from django.contrib import adminfrom django.urls import path, includefrom rest_framework_simplejwt.views import TokenVerifyView, TokenObtainPairView, TokenRefreshViewurlpatterns = [path('admin/', admin.site.urls),path('', include('django_app.urls')),path('auth/', include('rest_framework.urls')),path('login/', TokenObtainPairView.as_view()),path('refresh/', TokenRefreshView.as_view()),path('profile/', TokenVerifyView.as_view())]

After all of that, you should now be able to run your server again with . Head on over to the Django localhost on port 8000 and try hitting the following endpoint: localhost:8000/admin. Login as the administrator user you created and try seeding a couple of Models with some data! This is one of the great perks about Django — is a lot of your CRUD methods are already built in for you (unlike Rails, where you would have define index, show, create, update and destroy methods).

A tip of the cap to you for setting up your first Django API with Auth! Happy coding with your app’s frontend!

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store