serializer

Django Rest Framework regroup queryset by a category

余生长醉 提交于 2020-06-27 12:23:28
问题 In the current project that i'm working on, i need to regroup (group) a queryset by category and put contents with same category in a list all provided together. I have the following model structure: class Category(models.Model): title = models.CharField(max_length=255) class Item(models.Model): title = models.CharField(max_length=255) category = models.ForeignKey(to="Category", verbose_name=_('category'), related_name='items', on_delete=models.SET_NULL, null=True, blank=True) I would like

How can I deserialize an array of objects in Symfony Serializer?

限于喜欢 提交于 2020-05-11 05:07:09
问题 Is is possible in Symfony Serializer to deserialize an array of objects in a property? I have a Boss class with the $Npc = [] property that needs to hold a array of Npc objects. I did see some examples in the documentation, but they do not state this feature. I have a json string with an array of NPC's For example: class Boss { private $Npc = []; /** * @return Npc[] */ public function getNpcs(): array { return $this->npcs; } } I am using php7.1 and symfony/serializer version ^3.3. Edit: I

How can I deserialize an array of objects in Symfony Serializer?

半腔热情 提交于 2020-05-11 05:06:03
问题 Is is possible in Symfony Serializer to deserialize an array of objects in a property? I have a Boss class with the $Npc = [] property that needs to hold a array of Npc objects. I did see some examples in the documentation, but they do not state this feature. I have a json string with an array of NPC's For example: class Boss { private $Npc = []; /** * @return Npc[] */ public function getNpcs(): array { return $this->npcs; } } I am using php7.1 and symfony/serializer version ^3.3. Edit: I

Uploading multiple images and nested json using multipart/form-data in Django REST Framework

ε祈祈猫儿з 提交于 2020-04-28 11:02:14
问题 I have a problem with parsing request.data in viewset. I have a model that can add multiple images depending on a product. I want to split the image from the incoming data, send product data to ProductSerializer, and after that send image to its serializer with product data and save it. I have two model, simply like this: def Product(models.Model): name = models.CharField(max_length=20) color = models.ForeignKey(Color, on_delete=models.CASCADE) def Color(models.Model): name = models.CharField

Uploading multiple images and nested json using multipart/form-data in Django REST Framework

微笑、不失礼 提交于 2020-04-28 11:02:11
问题 I have a problem with parsing request.data in viewset. I have a model that can add multiple images depending on a product. I want to split the image from the incoming data, send product data to ProductSerializer, and after that send image to its serializer with product data and save it. I have two model, simply like this: def Product(models.Model): name = models.CharField(max_length=20) color = models.ForeignKey(Color, on_delete=models.CASCADE) def Color(models.Model): name = models.CharField

Exclude null properties in JMS Serializer

半城伤御伤魂 提交于 2020-01-14 17:55:54
问题 My consumed XML API has an option to retrieve only parts of the response. This causes the resulting object to have a lot of NULL properties if this feature is used. Is there a way to actually skip NULL properties? I tried to implement an exclusion strategy with shouldSkipProperty(PropertyMetadata $property, Context $context)` but i realized there is no way to access the current property value. An example would be the following class class Hotel { /** * @Type("string") */ public $id; /** *

Exclude null properties in JMS Serializer

橙三吉。 提交于 2020-01-14 17:54:17
问题 My consumed XML API has an option to retrieve only parts of the response. This causes the resulting object to have a lot of NULL properties if this feature is used. Is there a way to actually skip NULL properties? I tried to implement an exclusion strategy with shouldSkipProperty(PropertyMetadata $property, Context $context)` but i realized there is no way to access the current property value. An example would be the following class class Hotel { /** * @Type("string") */ public $id; /** *

WCF DataContracts

南笙酒味 提交于 2020-01-10 10:34:09
问题 I have a WCF service hosted for internal clients - we have control of all the clients. We will therefore be using a data contracts library to negate the need for proxy generation. I would like to use some readonly properties and have some datacontracts without default constructors. Thanks for your help... 回答1: Readonly properties are fine as long as you mark the (non-readonly) field as the [DataMember], not the property. Unlike XmlSerializer, IIRC DataContractSerializer doesn't use the

Use the “circular_reference_handler” key of the context instead symfony 4.2

时光怂恿深爱的人放手 提交于 2020-01-02 15:05:14
问题 I have to serialize an object and I get the ever so common "circular reference error" I have used the old Symfony method : $normalizer = new ObjectNormalizer(); // Add Circular reference handler $normalizer->setCircularReferenceHandler(function ($object) { return $object->getId(); }); $normalizers = array($normalizer); $encoders = [new JsonEncoder()]; $serializer = new Serializer($normalizers, $encoders); This work but as of Symfony 4.2 I get the exception you see in the title of this

Django rest framework: Insert multiple objects in one post request

∥☆過路亽.° 提交于 2020-01-01 07:19:12
问题 I am using DRF for my API backend. I need to insert multiple objects into one post request. I saw so many tutorials, as well How do I create multiple model instances with Django Rest Framework?, but not success. I am using ModelSerializer, but when using many=True then have problem with ListSerializer. views.py class SaleUserViewSet(ModelViewSet): queryset = SaleUser.objects.all() serializer_class = SaleUserSerializer(many=True) serlializers.py class SaleUserSerializer(serializers