marshmallow-jsonapi:如何重用架构但更改 `type_`?
marshmallow-jsonapi: How to reuse schema but change the `type_`?
我有很多端点,但它们使用相同的 jsonapi 架构。
POST /foods
{
"data": {
"type": "food",
"attributes": {
"name": "abc",
"item": "food"
}
}
}
POST /chats
{
"data": {
"type": "food",
"attributes": {
"name": "bcd",
"item": "chat"
}
}
}
POST /animals
{
"data": {
"type": "food",
"attributes": {
"name": "poi",
"item": "animals"
}
}
}
我的架构是:
import marshmallow_jsonapi as jsonapi
ItemSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "items"
strict = True
遵循 jsonapi 标准,type
将需要匹配每个端点(foods
、chats
、animals
)。
import marshmallow_jsonapi as jsonapi
FoodSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "foods"
strict = True
import marshmallow_jsonapi as jsonapi
ChatSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "chats"
strict = True
import marshmallow_jsonapi as jsonapi
AnimalSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "animals"
strict = True
有什么方法可以减少冗余代码,使 ItemSchema
的一堆副本只更改 type
?
我晚了一年。但是您可以从 BaseSchema
扩展并仅更改派生架构中的值
您可以在此处查看有关扩展架构和减少代码冗余的文档 https://marshmallow.readthedocs.io/en/latest/extending.html#custom-class-meta-options
这是基础class (BaseClass.py)
from marshmallow_jsonapi import fields, Schema
class Base(Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
您可以在其他模式中导入基础class
from BaseClass import Base
FoodSchema(Base):
# add other fields if required
class Meta:
type_ = "foods"
strict = True
Other Schema you need with same fields
from Base import Base
ChatSchema(Base):
# add other fields if required
class Meta:
type_ = "chats"
strict = True
我有很多端点,但它们使用相同的 jsonapi 架构。
POST /foods
{
"data": {
"type": "food",
"attributes": {
"name": "abc",
"item": "food"
}
}
}
POST /chats
{
"data": {
"type": "food",
"attributes": {
"name": "bcd",
"item": "chat"
}
}
}
POST /animals
{
"data": {
"type": "food",
"attributes": {
"name": "poi",
"item": "animals"
}
}
}
我的架构是:
import marshmallow_jsonapi as jsonapi
ItemSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "items"
strict = True
遵循 jsonapi 标准,type
将需要匹配每个端点(foods
、chats
、animals
)。
import marshmallow_jsonapi as jsonapi
FoodSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "foods"
strict = True
import marshmallow_jsonapi as jsonapi
ChatSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "chats"
strict = True
import marshmallow_jsonapi as jsonapi
AnimalSchema(jsonapi.Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
class Meta:
type_ = "animals"
strict = True
有什么方法可以减少冗余代码,使 ItemSchema
的一堆副本只更改 type
?
我晚了一年。但是您可以从 BaseSchema
扩展并仅更改派生架构中的值
您可以在此处查看有关扩展架构和减少代码冗余的文档 https://marshmallow.readthedocs.io/en/latest/extending.html#custom-class-meta-options
这是基础class (BaseClass.py)
from marshmallow_jsonapi import fields, Schema
class Base(Schema):
id = jsonapi.fields.UUID(dumps_only=True)
name = jsonapi.fields.String(required=True)
item = jsonapi.fields.String(required=True)
您可以在其他模式中导入基础class
from BaseClass import Base
FoodSchema(Base):
# add other fields if required
class Meta:
type_ = "foods"
strict = True
Other Schema you need with same fields
from Base import Base
ChatSchema(Base):
# add other fields if required
class Meta:
type_ = "chats"
strict = True