sttpforever

Untitled

Jun 12th, 2021
30
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. это вью файл
  2. from .serializers import PostSerializer
  3. from rest_framework import viewsets
  4.  
  5. from .models import Post
  6.  
  7. class PostViewSet(viewsets.ModelViewSet):
  8. queryset = Post.objects.all()
  9. serializer_class = PostSerializer
  10.  
  11. это сериалазер файл
  12. from rest_framework import serializers
  13. from .models import Post
  14.  
  15. class PostSerializer(serializers.ModelSerializer):
  16. class Meta:
  17. fields = ('id', 'text', 'author', 'image', 'pub_date')
  18. model = Post
  19.  
  20. Это головной урл
  21. from django.contrib import admin
  22. from django.urls import path, include
  23. from posts.views import PostViewSet
  24. from rest_framework.routers import DefaultRouter
  25. router = DefaultRouter()
  26. router.register('posts', PostViewSet)
  27.  
  28. urlpatterns = [
  29. path('admin/', admin.site.urls),
  30. path('', include('posts.urls')),
  31. path('', include(router.urls)),
  32. path('api/v1/posts/', include(router.urls)),
  33. path('api/v1/posts/<int:pk>/', include(router.urls)),
  34. ]
  35.  
  36. это урл приложения posts
  37. from django.urls import path, include
  38. from posts.views import PostViewSet
  39. from rest_framework.routers import DefaultRouter
  40. router = DefaultRouter()
  41. router.register('posts', PostViewSet)
  42. router.register('posts', PostViewSet)
  43.  
  44. urlpatterns = [
  45. path('', include(router.urls)),
  46. path('api/v1/posts/', include(router.urls)),
  47. path('api/v1/posts/<int:pk>/', include(router.urls)),
  48. ]
RAW Paste Data