drf 序列化器

2021-10-06 08:29:41 字數 3718 閱讀 1406

models.py

class

chargepolicydetail

(models.model)

:purpose

= c_models.apartment.

purpose

category

= c_models.apartment.

category

name = models.

charfield

( max_length=30,

blank=true,

null

=true,

verbose_name=

"收費明細"

) effect_start_date = models.

datefield

(default

=datetime.date.

today()

, blank=true,

null

=true,

verbose_name=

"生效開始時間"

) effect_end_date = models.

datefield

( blank=true,

null

=true,

verbose_name=

"生效結束時間"

) # 過濾項

purposes = models.

charfield

( max_length=30,

choices=

purpose

, blank=true,

null

=true,

verbose_name=

"建築用途"

) category = models.

charfield

( max_length=30,

choices=

category

, blank=true,

null

=true,

verbose_name=

"戶型"

) has_evl = models.

booleanfield

( blank=true,

null

=true,

verbose_name=

"有無電梯"

) property_rights = models.

charfield

( max_length=30,

blank=true,

null

=true,

choices=((

"0",

"民用"),

("1"

,"公用"))

, verbose_name=

"產權性質"

) formula = models.

charfield

( max_length=30,

blank=true,

null

=true,

verbose_name=

"計算公式"

) cycle = models.

charfield

( max_length=8,

choices=((

"1",

"每年"),

("2"

,"每半年"),

("3"

,"每月"),

),blank=true,

null

=true,

verbose_name=

"生成週期"

) charge_policy = models.

foreignkey

( to=chargepolicy,

blank=true,

null

=true,

on_delete=models.

cascade

, verbose_name=

"對應收費規則"

) policy = models.

foreignkey

( to=chargecategory,

blank=true,

null

=true,

on_delete=models.

cascade

, verbose_name=

"對應規則類目"

) updated_at = models.

datetimefield

(auto_now=true, verbose_name=

"更新時間"

) created_at = models.

datetimefield

(default

=timezone.now,

editable=false,

blank=true,

null

=true,

verbose_name=

"建立時間"

)class

meta

: verbose_name =

"收費規則"

class

chargecategory

(models.model)

: name = models.

charfield

(max_length=

16, blank=true,

null

=true, unique=true, verbose_name=

"類目名稱"

)

serializers.py
class

chargepolicydetailserializer

(serializers.modelserializer):""

"收費規則明細序列化"

"" policy = serializers.

charfield

(source=

"policy.name"

) purposes = serializers.

charfield

(source=

'get_purposes_display'

) category = serializers.

charfield

(source=

'get_category_display'

) property_rights = serializers.

charfield

(source=

'get_property_rights_display'

) cycle = serializers.

charfield

(source=

'get_cycle_display'

)class

meta

: model = models.chargepolicydetail

exclude =

("updated_at"

,"created_at"

)

drf 序列化器

序列化器 和 django中 的 form 和 modelform 有類似的地方 因為 在 drf 中,沒有模板的概念,所以 序列化的主要作用 是對響應的資料做序列化的,還可以對 接收到的資料做序列化 serializer 序列化 查詢的結果queryset user.objects.all ser...

drf之序列化器

1.drf框架定義的序列化流程 1.狹義上的序列化定義 資料流向 後端到前段 後端負責處理資料處理流程 模型類物件 字典 json字串 drf約定的序列化流程 1.通過django模型類獲取資料庫資料構建模型類物件 2.把模型類物件轉化為字典 3.把字典轉化為json格式的字串 2.drf框架的反序...

drf 中的序列化器

序列化器 和 django中 的 form 和 modelform 有類似的地方 因為 在 drf 中,沒有模板的概念,所以 序列化的主要作用 是對響應的資料做序列化的,還可以對 接收到的資料做序列化 serializer 序列化 查詢的結果queryset user.objects.all ser...