django-serializer

What types of validations are automatically handled by Django Rest Framework?

£可爱£侵袭症+ 提交于 2019-12-11 06:12:09
问题 Lets say I have a model defined as follows: from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator alphanumeric_validator = RegexValidator(r'^[a-zA-Z0-9]*$', 'Only alphanumeric characters are allowed.') class Person(model.Model): name = models.CharField(max_length=60, validators=[alphanumeric_validator]) number = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)]) email = models.EmailField() Now, lets say I am serializing and

Error uploading image using postman in django rest framework

独自空忆成欢 提交于 2019-12-11 04:11:25
问题 I'm trying to create an endpoint to upload images(using postman) to a specific folder using django rest framework. This is my settings for the folder, MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') This is my model, class UserMedia(models.Model): user = models.OneToOneField(User, related_name='medias', on_delete=models.CASCADE, ) profile_image_web = models.FileField(null=True) profile_image_android = models.FileField(null=True) profile_image_ios = models.FileField(null

How to update multiple records at once (bulk update) in django API

断了今生、忘了曾经 提交于 2019-12-10 17:26:04
问题 I need to update categories in many Article in one request. In ArticleViewSet I have: def get_serializer_class(self): if self.action in ['partial_update', 'update']: return ArticlePostSerializer return ArticleSerializer So ArticlePostSerializer need to be changed. This is my serializers code: class ArticleShortCategorySerializer(serializers.ModelSerializer): class Meta: model = Category fields = 'id', 'name' class ArticleSerializer(serializers.ModelSerializer): categories = serializers

Django Rest Framework serializer create() doesn't get triggered

烂漫一生 提交于 2019-12-10 15:58:10
问题 I have the following serializer class MyModelSerializer(serializers.ModelSerializer): user = UserSerializer() def create(self, validated_data): print("TEST") MyModel, created = MyModel.objects.get_or_create(**validated_data) return MyModel class Meta: model = MyModel fields = ('pk', 'title', 'user', 'movie', 'timestamp', 'text',) and the following viewset: class MyModelViewSet(viewsets.ModelViewSet): queryset = MyModel.objects.all() serializer_class = MyModelSerializer When I make an POST

How to add userprofile to UserDetailsSerializer in django

ぐ巨炮叔叔 提交于 2019-12-08 09:43:37
问题 Trying to add userprofile to user model using : django rest framework. rest-auth module But line profile = instance.userprofile giving error : *** django.db.models.fields.related.RelatedObjectDoesNotExist: User has no userprofile. following instructions from here Also, not sure on what is happening in super statement Possible errors: 1. instance is not having userprofile after the super statement, hence profile = instance.userprofile statement giving error 2. userprofile needs to be added to

Customize Json Response in django rest framework

心不动则不痛 提交于 2019-12-07 23:05:22
问题 Given below is my serializer class. I have all fields in one model.I would like to change the representation of serializer data in custom format. Tried to_representation method of serializer but could not success. class MyListSerilizer(ModelSerializer): class Meta: model=MyModel fields=['Name','Address_ID','Portal','Address','City','DisplayOrderDateTime','Email','Order_ID','Order_Code','Item_Code','DispatchedOn','Payment_Mode','Shipping_Charge','ShippingMethodCode','ShippingMethodCharges',

Return display_name in ChoiceField

 ̄綄美尐妖づ 提交于 2019-12-07 20:33:37
问题 I'm implementing some REST API in DRF with ModelViewSet and ModelSerializer . All my APIs use the JSON format and some of my models use ChoiceField field, like that: MyModel(models.Model): KEY1 = 'Key1' KEY2 = 'Key2' ATTRIBUTE_CHOICES = ( (KEY1, 'Label 1'), (KEY2, 'Label 2')) attribute = models.CharField(max_length=4, choices=ATTRIBUTE_CHOICES, default=KEY1) My problem is that by default DRF always returns (and accept) the key of these choices for JSON messages (see here), but I would like to

DjangoRestFramework ModelSerializer: field-level validation is not working

喜欢而已 提交于 2019-12-07 05:02:26
问题 This is my serializers.py (I want to create a serializer for the built-in User model): from rest_framework import serializers from django.contrib.auth.models import User class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ('username', 'password', 'email', ) def validate_username(self, username): if not re.search(r'^\w+$', username): #checks if all the characters in username are in the regex. If they aren't, it returns None raise serializers.ValidationError(

Change a field in a Django REST Framework ModelSerializer based on the request type?

浪尽此生 提交于 2019-12-06 18:04:41
问题 Consider this case where I have a Book and Author model. serializers.py class AuthorSerializer(serializers.ModelSerializer): class Meta: model = models.Author fields = ('id', 'name') class BookSerializer(serializers.ModelSerializer): author = AuthorSerializer(read_only=True) class Meta: model = models.Book fields = ('id', 'title', 'author') viewsets.py class BookViewSet(viewsets.ModelViewSet): queryset = Book.objects.all() serializer_class = BookSerializer This works great if I send a GET

Return display_name in ChoiceField

两盒软妹~` 提交于 2019-12-06 12:14:03
I'm implementing some REST API in DRF with ModelViewSet and ModelSerializer . All my APIs use the JSON format and some of my models use ChoiceField field, like that: MyModel(models.Model): KEY1 = 'Key1' KEY2 = 'Key2' ATTRIBUTE_CHOICES = ( (KEY1, 'Label 1'), (KEY2, 'Label 2')) attribute = models.CharField(max_length=4, choices=ATTRIBUTE_CHOICES, default=KEY1) My problem is that by default DRF always returns (and accept) the key of these choices for JSON messages (see here ), but I would like to use the label instead, because I think it's more consistent and clear to unterstand for who will use