How to check model in django rest framework?

I have a model that is essentially a single table with four lookup tables. One of the lookup tables is to specify the type. Based on the type (of which there are four), the requirements for the fields change. For example, if the type is Poll, the number_of_unique_contacts field is required, but not for any other type.

I examined the use of multiple tables and a database template based on inheritance modeling. But this does not make sense, since there are only four types, and only 6 fields are in the game as “contextually required”, depending on the type. In doing so, I would be open to several models inside Django, but I prefer the REST infrastructure to represent only one URI (not one for TYPE).

The question is: what is the best way to test a model for POST / PUT requests? Am I better off choosing a different database schema (as already mentioned, I think I have what I like)? Should I restructure my python model (again, one main class with four searches)?

I am new to Django and python, so please be careful (.NET / Java background). And thanks in advance

I think this is appropriate code (to be honest, the code probably does not matter, since it is more a design issue, but I always feel a strange post without code - when renting for context)

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 
+6
source share
1 answer

Adding a validation method to a serializer class is one option.

 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 
+2
source

Source: https://habr.com/ru/post/959414/


All Articles