最近,当我阅读Django rest完整API文档时,我遇到了以下代码:
def get(self, request, format=None):
"""Return a list of APIView features."""
an_apiview = [
'Uses HTTP methods as function (get, post, patch, put, delete).',
'It is similar to a traditional Django
如果我有这样的看法:
class MyAPIView(APIView):
def get(self, request, name=None):
return {"hello": name or "world"}
如何将其包含在生成的文档中?具体来说,我如何将它包含在API根中,所以当我访问"“时,它就会出现?
带有描述的APIView的APIView,但没有描述实际将其包含在API浏览器中的过程。
在我的项目中,我希望使用序列化类获得图像的完整路径。
当我使用ViewSet类时,它似乎得到了正确的结果。但是当使用APIView类时,它并没有得到我所需要的。
# the model
class UserAccount(models.Model):
...
icon = models.ImageField(upload_to='icon/user')
# the serializer
class UserSerializer(serialziers.ModelSerializer):
class Meta:
model = UserAc
我构建了这样一个APIView:
class CustomAPIView(APIView):
def get(self, request, *args, **kwargs):
if not request.user or not request.user.is_authenticated():
return Response("User not logged in", status=status.HTTP_403_FORBIDDEN)
# Other stuff
在我的html模板中,我使用fetchAPI调用它:
fe
我正在尝试用Django Rest框架做一个APIView。当我将视图与url关联时,我得到了这个错误: AssertionError: basename argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute. 下面是我的APIView: class ExampleView(APIView):
authentication_classes = (SessionAuthentica
我是DRF新手。在序列化程序类中定义HyperlinkedRelatedField时,如下所示: class JournalistSerializer(serializers.ModelSerializer):
articles = serializers.HyperlinkedRelatedField(view_name="article-
detail") 我收到以下错误: `HyperlinkedRelatedField` requires the request in the serializer
cont
当我向邮递员发送邮件请求时,我会收到一个错误Forbidden (CSRF cookie not set.)
class BooksView(View):
def post(self, request):
如果我使用csrf_exempt,则不会发生错误
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
@method_decorator(csrf_exempt, name='dispatch')
class
我正在为一个文档应用程序创建一个基于类的应用程序接口,但是我想在我的APIView中为post和patch定义添加特定的权限。例如,
class DocumentList(APIView):
def get(self,request,format=None):
... blah
def post(self,request,format=None):
only allow administrators to create new documents
... blah
我正在尝试查询一个模型,以检查一个对象是否存在,是否使用以下APIview函数。不幸的是,我无法接收任何响应对象来响应本机apisauce get函数,如下所示
apisauce函数
const checkIfUserExists = (email) =>
client.get("/account/user-exists/?email=" + email);
APIView类
class UserExistsView(APIView):
"""Checks if user exists or not"""
我在Django Rest中有一个用于用户配置文件的序列化程序:
class UserProfileSerializer(serializers.ModelSerializer):
......................
......................
status = serializers.SerializerMethodField()
def get_status(self, obj):
user = self.context['request'].user
if obj.use
在我所见过的Serialezers.py文件中,用户正在一次使用APIView,很少使用Modelviewset。
from django.shortcuts import render
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.utils import serializer_helpers
from rest_framework.views import APIView
from rest_framework.viewsets
我有一个APIView对象,如下所示:
from rest_framework.views import APIView
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
class SimulationGenerator(APIView):
@swagger_auto_schema()
def post(self, request) -> Response:
"""create a simula
我基于rest_framework.views.APIView定义了一个类,并将其作为视图添加到某些url中。例如:
from rest_framework.views import APIView
from rest_framework.response import Response
from django.urls import path
class MyApi(APIView):
def post(self, request):
# Do something with the request
print(request.data)
当我想序列化我的模型以获得它们的对象/记录的列表时,我会遇到关于何时使用APIView和何时使用ModelViewSet的区别?
例如,在中,通过ListUser类和它们的get方法,我们可以获得用户列表
class ListUsers(APIView):
"""
View to list all users in the system.
* Requires token authentication.
* Only admin users are able to access this view.
"""
我正在尝试构建一个简单的REST API。我尝试添加一个视图集,不知何故,我得到一个错误,没有这样的属性。如果我删除视图集,只使用APIView运行,它会加载得很好。我被卡住了。可能的问题是什么?我该怎么做才能让它工作呢?
下面是rest_profiles.views.py文件:
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
f