class BasicFieldsSerializer(serializers.ModelSerializer): form_titles = { 'table': 'Basic fields list', 'new': 'New basic fields object', 'edit': 'Editing basic fields object', } form_template = 'examples/form_cols.html' actions = Actions(TableAction(TablePosition.HEADER, _('Modal dialog'), title=_('Dialog test'), name='modal_dialog', action_js="examples.testModalDialog();"), add_default_crud=True, add_form_buttons=True) boolean_field = fields.BooleanField() nullboolean_field = fields.NullBooleanField() char_field = fields.CharField() email_field = fields.EmailField() slug_field = fields.SlugField() url_field = fields.URLField() uuid_field = fields.UUIDField() ipaddress_field = fields.IPAddressField() integer_field = fields.IntegerField() nullint_field = fields.IntegerField(allow_null=True) float_field = fields.FloatField() decimal_field = fields.DecimalField(max_digits=5, decimal_places=2) datetime_field = fields.DateTimeField(required=False) date_field = fields.DateField() time_field = fields.TimeField() duration_field = fields.DurationField() password_field = fields.CharField(password_field=True) class Meta: model = BasicFields exclude = ()
class AdvancedFieldsSerializer(serializers.ModelSerializer): form_titles = { 'table': 'Advanced fields list', 'new': 'New advanced fields object', 'edit': 'Editing advanced fields object', } regex_pattern = '(?<=abc)def' regex_field = fields.RegexField( regex_pattern, error_messages={ 'invalid': 'This value does not match the required pattern {regex_pattern}.'. format(**locals()) }) choice_field = fields.ChoiceField(choices=( ('0', 'Choice 1'), ('1', 'Choice 2'), ('2', 'Choice 3'), ('3', 'Choice 4'), )) single_choice_field = fields.ChoiceField(choices=(('0', 'Choice 1'), ), single_choice_hide=True) hidden_field = fields.DateTimeField(default=timezone.now, display=fields.DisplayMode.HIDDEN) readonly_field = fields.BooleanField(read_only=True) filepath_field = fields.FilePathField(path='examples') # TODO: MultipleChoiceField # Problem: Not saved properly to the database. Saved as 'set()' string. Why? # multiplechoice_field = serializers.MultipleChoiceField(choices=( # (0, 'Choice 1'), # (1, 'Choice 2'), # (2, 'Choice 3'), # (3, 'Choice 4'), # )) # TODO: FileField, ImageField # image_field = serializers.ImageField(required=False, use_url=True) # Error: The submitted data was not a file. Check the encoding type on the form. # Hints: # base_form.html: ectype="multipart/form-data" set on form # settings.py: FormParser & MultiPartParser set as DEFAULT_PARSER_CLASSES # urls.py: media folder added to urlpatterns (+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)) # dynamicforms.js: contentType='multipart/form-data' in submitForm() # see how data is processed in ajax-form.js in DRF in case contentType='multipart/form-data' is used """ListField and DictField not supported in HTML forms in DRF""" # list_field = serializers.ListField() # dict_field = serializers.DictField() """JSONField available only for PostgreSQL""" # json_field = serilaizers.JSONField() """ SerializerMethodField This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object. """ # serializer_method_field = serializers.SerializerMethodField() """ ModelField A generic field that can be tied to any arbitrary model field. The ModelField class delegates the task of serialization/deserialization to its associated model field. This field can be used to create serializer fields for custom model fields, without having to create a new custom serializer field. """ # model_field = serializers.ModelField() # Relations string_related_field = fields.StringRelatedField( source='primary_key_related_field') primary_key_related_field = fields.PrimaryKeyRelatedField( queryset=Relation.objects.all()) slug_related_field = fields.SlugRelatedField( slug_field='name', queryset=Relation.objects.all()) file_field = DfFileField(max_length=None, allow_empty_file=False, use_url=False) # hyperlinked_related_field = serializers.HyperlinkedRelatedField(view_name='relation-detail', read_only=True) # hyperlinked_identity_field = serializers.HyperlinkedIdentityField(view_name='relation-detail', read_only=True) class Meta: model = AdvancedFields exclude = ('multiplechoice_field', 'image_field', 'hyperlinked_related_field', 'hyperlinked_identity_field') def create(self, validated_data): return AdvancedFields.objects.create(**validated_data)