mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 01:20:02 +03:00
add detail file path
This commit is contained in:
parent
ac0f0a1774
commit
eecd5cea5c
|
@ -35,7 +35,7 @@ The wrappers also provide behaviour such as returning `405 Method Not Allowed` r
|
|||
|
||||
Okay, let's go ahead and start using these new components to write a few views.
|
||||
|
||||
We don't need our `JSONResponse` class in `views.py` any more, so go ahead and delete that. Once that's done we can start refactoring our views slightly.
|
||||
We don't need our `JSONResponse` class in `snippets/views.py` any more, so go ahead and delete that. Once that's done we can start refactoring our views slightly.
|
||||
|
||||
from rest_framework import status
|
||||
from rest_framework.decorators import api_view
|
||||
|
@ -63,7 +63,7 @@ We don't need our `JSONResponse` class in `views.py` any more, so go ahead and d
|
|||
|
||||
Our instance view is an improvement over the previous example. It's a little more concise, and the code now feels very similar to if we were working with the Forms API. We're also using named status codes, which makes the response meanings more obvious.
|
||||
|
||||
Here is the view for an individual snippet, in the `views.py` module.
|
||||
Here is the view for an individual snippet, in the `snippets/views.py` module.
|
||||
|
||||
@api_view(['GET', 'PUT', 'DELETE'])
|
||||
def snippet_detail(request, pk):
|
||||
|
|
|
@ -30,7 +30,7 @@ We'll start by rewriting the root view as a class-based view. All this involves
|
|||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `views.py`.
|
||||
So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in `snippets/views.py`.
|
||||
|
||||
class SnippetDetail(APIView):
|
||||
"""
|
||||
|
@ -83,7 +83,7 @@ One of the big wins of using class-based views is that it allows us to easily co
|
|||
|
||||
The create/retrieve/update/delete operations that we've been using so far are going to be pretty similar for any model-backed API views we create. Those bits of common behaviour are implemented in REST framework's mixin classes.
|
||||
|
||||
Let's take a look at how we can compose the views by using the mixin classes. Here's our `views.py` module again.
|
||||
Let's take a look at how we can compose the views by using the mixin classes. Here's our `snippets/views.py` module again.
|
||||
|
||||
from snippets.models import Snippet
|
||||
from snippets.serializers import SnippetSerializer
|
||||
|
@ -126,7 +126,7 @@ Pretty similar. Again we're using the `GenericAPIView` class to provide the cor
|
|||
|
||||
## Using generic class-based views
|
||||
|
||||
Using the mixin classes we've rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use to trim down our `views.py` module even more.
|
||||
Using the mixin classes we've rewritten the views to use slightly less code than before, but we can go one step further. REST framework provides a set of already mixed-in generic views that we can use to trim down our `snippets/views.py` module even more.
|
||||
|
||||
from snippets.models import Snippet
|
||||
from snippets.serializers import SnippetSerializer
|
||||
|
|
|
@ -12,7 +12,7 @@ Currently our API doesn't have any restrictions on who can edit or delete code s
|
|||
We're going to make a couple of changes to our `Snippet` 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`.
|
||||
Add the following two fields to the `Snippet` model in `snippets/models.py`.
|
||||
|
||||
owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
|
||||
highlighted = models.TextField()
|
||||
|
@ -54,7 +54,7 @@ You might also want to create a few different users, to use for testing the API.
|
|||
|
||||
## 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. In `serializers.py` add:
|
||||
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. In `snippets/serializers.py` add:
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
@ -67,7 +67,7 @@ Now that we've got some users to work with, we'd better add representations of t
|
|||
|
||||
Because `'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.
|
||||
We'll also add a couple of views to `snippets/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.
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
@ -81,7 +81,7 @@ We'll also add a couple of views to `views.py`. We'd like to just use read-only
|
|||
queryset = User.objects.all()
|
||||
serializer_class = UserSerializer
|
||||
|
||||
Make sure to also import the `UserSerializer` class
|
||||
Make sure to also import the `UserSerializer` class in `snippets/serializers.py`:
|
||||
|
||||
from snippets.serializers import UserSerializer
|
||||
|
||||
|
@ -96,7 +96,7 @@ Right now, if we created a code snippet, there'd be no way of associating the us
|
|||
|
||||
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:
|
||||
On the `SnippetList` view class, add the following method in `snippets/views.py`:
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(owner=self.request.user)
|
||||
|
@ -105,7 +105,7 @@ The `create()` method of our serializer will now be passed an additional `'owner
|
|||
|
||||
## Updating our serializer
|
||||
|
||||
Now that snippets are associated with the user that created them, let's update our `SnippetSerializer` to reflect that. Add the following field to the serializer definition in `serializers.py`:
|
||||
Now that snippets are associated with the user that created them, let's update our `SnippetSerializer` to reflect that. Add the following field to the serializer definition in `snippets/serializers.py`:
|
||||
|
||||
owner = serializers.ReadOnlyField(source='owner.username')
|
||||
|
||||
|
@ -121,7 +121,7 @@ Now that code snippets are associated with users, we want to make sure that only
|
|||
|
||||
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 is `IsAuthenticatedOrReadOnly`, 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
|
||||
First add the following import in `snippets/views.py`.
|
||||
|
||||
from rest_framework import permissions
|
||||
|
||||
|
@ -133,7 +133,7 @@ Then, add the following property to **both** the `SnippetList` and `SnippetDetai
|
|||
|
||||
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-level `urls.py` file.
|
||||
We can add a login view for use with the browsable API, by editing the URLconf in our project-level `tutorial/urls.py` file.
|
||||
|
||||
Add the following import at the top of the file:
|
||||
|
||||
|
@ -149,7 +149,7 @@ The `'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.
|
||||
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
|
||||
|
||||
|
@ -157,7 +157,7 @@ Really we'd like all code snippets to be visible to anyone, but also make sure t
|
|||
|
||||
To do that we're going to need to create a custom permission.
|
||||
|
||||
In the snippets app, create a new file, `permissions.py`
|
||||
In the snippets app, create a new file, `snippets/permissions.py`
|
||||
|
||||
from rest_framework import permissions
|
||||
|
||||
|
@ -176,7 +176,7 @@ In the snippets app, create a new file, `permissions.py`
|
|||
# Write permissions are only allowed to the owner of the snippet.
|
||||
return obj.owner == request.user
|
||||
|
||||
Now we can add that custom permission to our snippet instance endpoint, by editing the `permission_classes` property on the `SnippetDetail` view class:
|
||||
Now we can add that custom permission to our snippet instance endpoint, by editing the `permission_classes` property on the `SnippetDetail` view class in `snippets/views.py`:
|
||||
|
||||
permission_classes = (permissions.IsAuthenticatedOrReadOnly,
|
||||
IsOwnerOrReadOnly,)
|
||||
|
|
|
@ -10,7 +10,7 @@ A `ViewSet` class is only bound to a set of method handlers at the last moment,
|
|||
|
||||
Let's take our current set of views, and refactor them into view sets.
|
||||
|
||||
First of all let's refactor our `UserList` and `UserDetail` views into a single `UserViewSet`. We can remove the two views, and replace them with a single class:
|
||||
First of all let's refactor our `UserList` and `UserDetail` views into a single `UserViewSet` in `snippets/views.py`. We can remove the two views, and replace them with a single class:
|
||||
|
||||
from rest_framework import viewsets
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user