没有项目描述
drf-nested的Python项目详细描述
DRF嵌套实用程序
这个包提供了一组实用程序来帮助开发人员实现django rest框架的嵌套数据处理。
此软件包增加了对以下内容的支持:
- 直接关系处理(
ForeignKey
) - 反向关系处理(即,允许使用当前为
ForeignKey
)的模型 - 直接和反向的
ManyToMany
,与定制through
模型的m2m关系有特殊流程
具有特殊混合项的LI> ^ {CD5> }
它还提供了用于处理^ {< CD6>}和^ {< CD7>}验证器的MIXIN。
< H2>混合蛋白< /H2>< H3>嵌套序列化混合器< EH3>BaseNestedMixin
MaskMIN,包含检索序列化模型的所有相关字段的方法。
它还为每种类型的字段提供了所有的update_or_create
方法
(direct relation
,reverse relation
,many-to-many relation
和generic relation
)。
CreateNestedMixin
混和,允许在序列化器^ {< CD15>}调用中创建嵌套模型。
您可以提供创建时应禁止的字段列表,
字段列表应该放在forbidden_on_create
序列化程序Meta
类上的字段。
MIXIN使用^ {CD8}}属性和^ {CD19}}方法来创建嵌套字段。
UpdateNestedMixin
MIXIN允许修改序列化器{{CD21>}调用的嵌套模型。 MIXIN使用^ {CD8}}属性和^ {CD19}}方法来更新嵌套字段。
< H3>验证器MIXIN < EH3>UniqueFieldMixin
MIXIN允许使用嵌套混合使用^ {< CD25>}字段。
这个混合器将验证过程从^ {< CD26> }移动到^ {CD27>}调用。
这是因为应该在unique
验证中使用的字段可能不是
在初始的is_valid
调用上设置,并在嵌套的create/update
调用之前设置。
UniqueTogetherMixin
MIXIN允许使用嵌套混合使用^ {< CD32 >}字段。
这个混合器将验证过程从^ {< CD26> }移动到^ {CD27>}调用。
这是因为应该在unique_together
验证中使用的字段可能不是
在初始的is_valid
调用上设置,并在嵌套的create/update
调用之前设置。
NestableMixin
MIXIN,如果字段的初始^ {< CD40>}不同,则允许通过设置^ {CD39>}指定嵌套字段的名称
从字段名或初始source
不可写(例如,属性)。
ThroughMixin
MIXIN允许指定^ {{CD4>}模型在^ {CD4>}模型^ {CD27>}之后连接到当前模型。
GenericRelationMixin
混和,应该用于表示由{{CD5>}模型连接的序列化器。
示例
您可以在examples/
目录中看到一个示例项目。
注释
If you are using a Many-to-Many field with
source
property or you have athrough
model on your serializer, you should add aNestableMixin
to the target serializer and add awrite_source
field when you initialize that serializer.
In case of the
source
property you should add an actual model field that would allow you to properly connect your model with related ones.
In case of the
through
model you should have it set to therelated_name
of the connectedthrough
model
You can also use
ThroughMixin
and setconnect_to_model
to False if you want to have the ability to keep thethrough
model connection in case thethrough
model ForeignKey should be different from the current model.