class UserCreateSerializer(serializers.ModelSerializer): username = serializers.CharField( required=True, validators=[ UniqueValidator(queryset=User.objects.all()), ]) password = serializers.CharField(write_only=True, required=True) profile = ProfileSerializer(many=False, required=False) pic = fields.ImageField(write_only=True, required=False) about = fields.CharField(write_only=True, required=False) def create(self, validated_data): password = validated_data.pop('password') pic = None about = None if 'pic' in validated_data: pic = validated_data.pop('pic') if 'about' in validated_data: about = validated_data.pop('about') user = User.objects.create(**validated_data) user.set_password(password) user.save() if pic is not None: user.profile.pic = pic if about is not None: user.profile.about = about user.profile.save() return user class Meta: model = User fields = ('id', 'username', 'password', 'email', 'profile', 'pic', 'about')
class ImageSerializer(Serializer): pk = fields.IntegerField() name = fields.CharField() url = fields.URLField() web_url = fields.ImageField() # image_thumbnail = fields.ImageField() variant = fields.CharField() main_image = fields.BooleanField()
class ProfileSerializer(SerializerMixin, Serializer): avatar = fields.ImageField(required=False) birthdate = fields.DateField(required=False) telephone = fields.CharField(required=False) address = fields.CharField(required=False) city = fields.CharField(required=False) zip_code = fields.CharField(required=False) def update(self, instance, validated_data): # Check if the user has the permission # to modify his user profile if not instance.myuser.has_perm('accounts.change_myuserprofile'): raise serializers.ValidationError('Permission denied', code='permission_denied') return super().update(self.instance, validated_data)
class Meta: param_fields = ( ('username', fields.CharField( label=_('login name'), max_length=64, help_text=_( 'Help text for field. It will show in api document'))), ('password', fields.CharField(label=_('password'))), ('gender', fields.ChoiceField(label=_('gender'), choices=constants.Gender.get_choices_list())), ('mobile', fields.RegexField(r'1\d{10}', label=_('mobile number'))), ('nickname', fields.CharField(label=_('nick name'), max_length=255)), ('name', fields.CharField(label=_('name'), default='', max_length=255)), ('avatar', fields.ImageField(label=_('avatar'), default=None)), )
class UserSerializer(serializers.ModelSerializer): password = serializers.CharField(write_only=True) profile = ProfileSerializer(many=False, required=False) pic = fields.ImageField(write_only=True, required=False) about = fields.CharField(write_only=True, required=False) def update(self, instance, validated_data): instance.email = validated_data.get('email', instance.email) instance.first_name = validated_data.get('first_name', instance.first_name) instance.last_name = validated_data.get('last_name', instance.first_name) if validated_data.get('password'): instance.set_password(validated_data.get('password')) instance.save() return instance class Meta: model = User fields = ('id', 'username', 'password', 'email', 'profile', 'pic', 'about')
class ProductSerializer(serializers.ModelSerializer): # category = CategoryWriteSerializer(read_only=True) # image = fields.HybridImageField(max_length=None, use_url=True, allow_null=True) image = fields.ImageField(allow_null=True, read_only=True) class Meta: model = Product fields = [ "id", "name", "brand", "category", "size", "color", "original_price", "price", "stock", "description", "image", "create_date", "last_modified", ]
class BitSerializer(serializers.ModelSerializer): bitset_logo = fields.ImageField(source='bitset.logo') bitset_slug = fields.CharField(source='bitset.slug') username = fields.CharField(source='creator.username') comments_count = serializers.SerializerMethodField() class Meta: model = models.Bit fields = ( 'title', 'bitset_logo', 'bitset_slug', 'bit_type', 'content', 'upvotes', 'downvotes', 'score', 'username', 'comments_count', ) def get_comments_count(self, instance): return instance.comments.count()
class TicketImageSerializer(serializers.ModelSerializer): image = fields.ImageField(read_only=True) class Meta: model = TicketImage fields = ('image', )