- Code snippets are always associated with a creator.
- Only authenticated users may create snippets.
- Only the creator of a snippet may update or delete it.
- Unauthenticated requests should have full read-only access.
Adding information to our model
We’re going to make a couple of changes to ourSnippet model class. First, let’s add a couple of fields. One of those fields will be used to represent the user who created the code snippet. The other field will be used to store the highlighted HTML representation of the code.
Add the following two fields to the Snippet model in models.py:
snippets/models.py
pygments code highlighting library.
We’ll need some extra imports:
snippets/models.py
.save() method to our model class:
snippets/models.py
createsuperuser command.
Adding endpoints for our User models
Now that we’ve got some users to work with, we’d better add representations of those users to our API. Creating a new serializer is easy. Inserializers.py add:
snippets/serializers.py
'snippets' is a reverse relationship on the User model, it will not be included by default when using the ModelSerializer class, so we needed to add an explicit field for it.
We’ll also add a couple of views to views.py. We’d like to just use read-only views for the user representations, so we’ll use the ListAPIView and RetrieveAPIView generic class-based views.
snippets/views.py
snippets/urls.py:
snippets/urls.py
Associating Snippets with Users
Right now, if we created a code snippet, there’d be no way of associating the user that created the snippet, with the snippet instance. The user isn’t sent as part of the serialized representation, but is instead a property of the incoming request. The way we deal with that is by overriding a.perform_create() method on our snippet views, that allows us to modify how the instance save is managed, and handle any information that is implicit in the incoming request or requested URL.
On the SnippetList view class, add the following method:
snippets/views.py
create() method of our serializer will now be passed an additional 'owner' field, along with the validated data from the request.
Updating our serializer
Now that snippets are associated with the user that created them, let’s update ourSnippetSerializer to reflect that. Add the following field to the serializer definition in serializers.py:
snippets/serializers.py
Make sure you also add
'owner', to the list of fields in the inner Meta class.source argument controls which attribute is used to populate a field, and can point at any attribute on the serialized instance. It can also take the dotted notation shown above, in which case it will traverse the given attributes, in a similar way as it is used with Django’s template language.
The field we’ve added is the untyped ReadOnlyField class, in contrast to the other typed fields, such as CharField, BooleanField etc. The untyped ReadOnlyField is always read-only, and will be used for serialized representations, but will not be used for updating model instances when they are deserialized. We could have also used CharField(read_only=True) here.
Adding required permissions to views
Now that code snippets are associated with users, we want to make sure that only authenticated users are able to create, update and delete code snippets. REST framework includes a number of permission classes that we can use to restrict who can access a given view. In this case the one we’re looking for isIsAuthenticatedOrReadOnly, which will ensure that authenticated requests get read-write access, and unauthenticated requests get read-only access.
First add the following import in the views module:
snippets/views.py
SnippetList and SnippetDetail view classes:
snippets/views.py
Built-in Permission Classes
AllowAny
Allow unrestricted access, regardless of authentication.
IsAuthenticated
Deny permission to any unauthenticated user.
IsAdminUser
Deny permission to any user unless
user.is_staff is True.IsAuthenticatedOrReadOnly
Authenticated users get full access, unauthenticated get read-only.
Adding login to the Browsable API
If you open a browser and navigate to the browsable API at the moment, you’ll find that you’re no longer able to create new code snippets. In order to do so we’d need to be able to login as a user. We can add a login view for use with the browsable API, by editing the URLconf in our project-levelurls.py file.
Add the following import at the top of the file:
tutorial/urls.py
tutorial/urls.py
'api-auth/' part of pattern can actually be whatever URL you want to use.
Now if you open up the browser again and refresh the page you’ll see a ‘Login’ link in the top right of the page. If you log in as one of the users you created earlier, you’ll be able to create code snippets again.
Once you’ve created a few code snippets, navigate to the ‘/users/’ endpoint, and notice that the representation includes a list of the snippet ids that are associated with each user, in each user’s ‘snippets’ field.
Object level permissions
Really we’d like all code snippets to be visible to anyone, but also make sure that only the user that created a code snippet is able to update or delete it. To do that we’re going to need to create a custom permission. In the snippets app, create a new file,permissions.py:
snippets/permissions.py
permission_classes property on the SnippetDetail view class:
snippets/views.py
Understanding Custom Permissions
Inherit from BasePermission
All custom permissions should inherit from
rest_framework.permissions.BasePermission.Implement permission methods
You can implement
has_permission(self, request, view) for view-level permissions and/or has_object_permission(self, request, view, obj) for object-level permissions.Authenticating with the API
Because we now have a set of permissions on the API, we need to authenticate our requests to it if we want to edit any snippets. We haven’t set up any authentication classes, so the defaults are currently applied, which areSessionAuthentication and BasicAuthentication.
When we interact with the API through the web browser, we can login, and the browser session will then provide the required authentication for the requests.
If we’re interacting with the API programmatically we need to explicitly provide the authentication credentials on each request.
If we try to create a snippet without authenticating, we’ll get an error:
Authentication Classes
REST framework provides several authentication schemes out of the box:SessionAuthentication
SessionAuthentication
Uses Django’s default session backend for authentication. Appropriate for AJAX clients running in the same session context as your website.
BasicAuthentication
BasicAuthentication
Signs requests using HTTP Basic Authentication. Generally only appropriate for testing.
TokenAuthentication
TokenAuthentication
Simple token-based HTTP Authentication scheme. Appropriate for client-server setups.
RemoteUserAuthentication
RemoteUserAuthentication
Allows you to delegate authentication to your web server.
