抱歉我的英文。我是django的新手,我想创建自定义帖子。我没有这个模型,我不需要创建它。我无法理解如何在django中创建正常的帖子请求。贝娄我尝试创造它
视图:
class CreateCustopPost(generics.GenericAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = CustomSerializer
串行器:
class CustomSerializer(serializers.ModelSerializer):
type_report = serializers.CharField(max_length=23)
client_token = serializers.CharField(max_length=128)
year_p_start = serializers.DecimalField(max_digits=10, decimal_places=2)
month_p_start = serializers.DecimalField(max_digits=10, decimal_places=2)
day_p_start = serializers.DecimalField(max_digits=10, decimal_places=2)
year_p_end = serializers.DecimalField(max_digits=10, decimal_places=2)
month_p_end = serializers.DecimalField(max_digits=10, decimal_places=2)
day_p_end = serializers.DecimalField(max_digits=10, decimal_places=2)
class Meta:
model = # i dont have model for this.
fields = ('type_report', 'client_token', 'year_p_start', 'month_p_start', 'day_p_start',
'year_p_end', 'month_p_end', 'day_p_end')
我的问题:需要在 model = 中键入序列化程序类?
答案 0 :(得分:2)
ModelSerializer
用于需要对模型执行某些操作的情况。 ModelSerializer自动创建与“模型”字段对应的字段。由于您没有任何模型,因此可以使用普通Serializer
类,在这种情况下,您不需要指定模型:
class CustomSerializer(serializers.Serializer):
type_report = serializers.CharField(max_length=23)
client_token = serializers.CharField(max_length=128)
year_p_start = serializers.DecimalField(max_digits=10, decimal_places=2)
month_p_start = serializers.DecimalField(max_digits=10, decimal_places=2)
day_p_start = serializers.DecimalField(max_digits=10, decimal_places=2)
year_p_end = serializers.DecimalField(max_digits=10, decimal_places=2)
month_p_end = serializers.DecimalField(max_digits=10, decimal_places=2)
day_p_end = serializers.DecimalField(max_digits=10, decimal_places=2)