mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 09:57:55 +03:00 
			
		
		
		
	Tweaked a few issues in the tutorial documentation.
This commit is contained in:
		
							parent
							
								
									4d9e7a5356
								
							
						
					
					
						commit
						8ccf5bcc0b
					
				| 
						 | 
					@ -191,7 +191,7 @@ Our `SnippetSerializer` class is replicating a lot of information that's also co
 | 
				
			||||||
In the same way that Django provides both `Form` classes and `ModelForm` classes, REST framework includes both `Serializer` classes, and `ModelSerializer` classes.
 | 
					In the same way that Django provides both `Form` classes and `ModelForm` classes, REST framework includes both `Serializer` classes, and `ModelSerializer` classes.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Let's look at refactoring our serializer using the `ModelSerializer` class.
 | 
					Let's look at refactoring our serializer using the `ModelSerializer` class.
 | 
				
			||||||
Open the file `snippets/serializers.py` again, and edit the `SnippetSerializer` class.
 | 
					Open the file `snippets/serializers.py` again, and replace the `SnippetSerializer` class with the following.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class SnippetSerializer(serializers.ModelSerializer):
 | 
					    class SnippetSerializer(serializers.ModelSerializer):
 | 
				
			||||||
        class Meta:
 | 
					        class Meta:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -64,7 +64,7 @@ That's looking good.  Again, it's still pretty similar to the function based vie
 | 
				
			||||||
 | 
					
 | 
				
			||||||
We'll also need to refactor our `urls.py` slightly now we're using class based views.
 | 
					We'll also need to refactor our `urls.py` slightly now we're using class based views.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    from django.conf.urls import patterns, url
 | 
					    from django.conf.urls import url
 | 
				
			||||||
    from rest_framework.urlpatterns import format_suffix_patterns
 | 
					    from rest_framework.urlpatterns import format_suffix_patterns
 | 
				
			||||||
    from snippets import views
 | 
					    from snippets import views
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -177,7 +177,7 @@ In the snippets app, create a new file, `permissions.py`
 | 
				
			||||||
            # Write permissions are only allowed to the owner of the snippet.
 | 
					            # Write permissions are only allowed to the owner of the snippet.
 | 
				
			||||||
            return obj.owner == request.user
 | 
					            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` class:
 | 
					Now we can add that custom permission to our snippet instance endpoint, by editing the `permission_classes` property on the `SnippetDetail` view class:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,
 | 
					    permission_classes = (permissions.IsAuthenticatedOrReadOnly,
 | 
				
			||||||
                          IsOwnerOrReadOnly,)
 | 
					                          IsOwnerOrReadOnly,)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -106,6 +106,8 @@ If we're going to have a hyperlinked API, we need to make sure we name our URL p
 | 
				
			||||||
 | 
					
 | 
				
			||||||
After adding all those names into our URLconf, our final `snippets/urls.py` file should look something like this:
 | 
					After adding all those names into our URLconf, our final `snippets/urls.py` file should look something like this:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    from django.conf.urls import url, include
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # API endpoints
 | 
					    # API endpoints
 | 
				
			||||||
    urlpatterns = format_suffix_patterns([
 | 
					    urlpatterns = format_suffix_patterns([
 | 
				
			||||||
        url(r'^$', views.api_root),
 | 
					        url(r'^$', views.api_root),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user