如何验证django rest框架中的模型?

时间:2013-12-05 18:16:32

标签: python django rest django-rest-framework

我的模型本质上是一个包含四个查找表的单个表。其中一个查找表是指定类型。根据类型(其中有四种),对字段的要求会发生变化。例如,如果类型为“Survey”,则字段number_of_unique_contacts是必需的,但不适用于任何其他类型。

我考虑过使用基于建模继承的多个表和数据库模式。但这没有意义,因为只有四种类型,并且只有6个字段作为“上下文需要”依赖于类型。话虽这么说,我会对Django中的多个模型开放,但我更喜欢REST框架只呈现一个URI(不是每个TYPE一个)。

手头的问题是:在POST / PUT请求上验证模型的最佳方法是什么?我最好选择另一个数据库模式(如上所述,我认为我对我感觉合适)?应该重组我的python模型(同样是一个有四个查找的主类)?

我是Django和python的新手,所以请保持温和(.NET / Java背景)。并提前谢谢

我认为相关的代码(老实说,代码并不重要,因为这更像是一个设计问题,但我总是觉得奇怪的发布没有代码 - 至少是为了上下文)

class MySerializer(serializers.ModelSerializer):
    proposal_side = serializers.SlugRelatedField(many=False, read_only=False, slug_field='proposal_side')
    my_proposal_type = serializers.SlugRelatedField(many=False, read_only=False, slug_field='proposal_type')
    my_proposal_delivery_type = serializers.SlugRelatedField(many=False, read_only=False, slug_field='delivery_type')
    my_survey_method = serializers.SlugRelatedField(many=False, read_only=False, slug_field='method')
    class Meta:
        model = diliModels.Proposal
        fields = (
            'id'
            ,'my_proposal_side'
            ,'my_proposal_type'
            ,'number_of_participants'
            ,'cost_per_participants'
            ,'minimum_dollar_commitment'
            ,'commercial_terms'
            ,'is_publicly_visible'
            ,'is_anonymous'
            ,'is_republish'
            ,'name'
            ,'my_delivery_type'
            ,'my_survey_method'
            ,'number_of_unique_contacts'
            ,'availability_start'
            ,'availability_end'
            ,'location_country'
            ,'location_city'
            ,'location_state'
            ,'description'
            ,'desired_meetings'
        )


class MyViewSet(viewsets.ModelViewSet):
    paginate_by = 100

    queryset = myModels\
        .MyProposal\
        .objects\
        .prefetch_related('blah')
    print 'SQL::MyViewSet: ' + str(queryset.query)
    serializer_class = serializers.MySerializer

1 个答案:

答案 0 :(得分:2)

向序列化程序类添加验证方法是一个选项

class MySerializer(serializers.ModelSerializer):
    proposal_side = serializers.SlugRelatedField(many=False, read_only=False, slug_field='proposal_side')
    my_proposal_type = serializers.SlugRelatedField(many=False, read_only=False, slug_field='proposal_type')
    my_proposal_delivery_type = serializers.SlugRelatedField(many=False, read_only=False, slug_field='delivery_type')
    my_survey_method = serializers.SlugRelatedField(many=False, read_only=False, slug_field='method')
    class Meta:
        model = diliModels.Proposal
        fields = (
            'id'
            ,'my_proposal_side'
            ,'my_proposal_type'
            ,'number_of_participants'
            ,'cost_per_participants'
            ,'minimum_dollar_commitment'
            ,'commercial_terms'
            ,'is_publicly_visible'
            ,'is_anonymous'
            ,'is_republish'
            ,'name'
            ,'my_delivery_type'
            ,'my_survey_method'
            ,'number_of_unique_contacts'
            ,'availability_start'
            ,'availability_end'
            ,'location_country'
            ,'location_city'
            ,'location_state'
            ,'description'
            ,'desired_meetings'
        )

    def validate(self, attrs):
         raise serializers.ValidationError("error")
         return attrs