我正在使用Django Rest框架,并尝试创建一个端点来创建一些对象,它不会被假设保存在DB中(managed = false)。 我想得到JSONApi'响应,所以我明白我应该为它创建一个模型和一个序列化器。
这是classView:
class OssObjectsViewSet(AuthenticatedCreateModelMixin, GenericViewSet):
serializer_class = OssObjectSerializer
def get_queryset(self):
m = OssObject()
return [m]
这是模型:
class OssObject(models.Model):
class Meta:
managed = False
class JSONAPIMeta:
resource_name = "oss_objects"
id = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True, unique=True, primary_key=True)
@property
def urn(self):
return 'yyy'
这是序列化程序:
class OssObjectSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField()
class Meta:
model = OssObject
fields = ['id', 'urn']
向终端发送Post请求时,我正在
{"data": {}, "message": "('relation \"submittals_ossobject\" does not exist\\nLINE 1: INSERT INTO \"submittals_ossobject\" (\"id\") VALUES (\\'81400b5b-...\\n ^\\n',)", "error_id": "8b9a3352-da41-4dd3-be09-5e63602aef46", "code": 10001}
使用' AuthenticatedListModelMixin'创建classView时并发送'获取'请求 - 它工作正常。