Thank you very much.

I've tried this approach but with ModelSerializer, and i couldn't find a 
field.
It was my mistake.
I'll try with Serializer.


пятница, 20 мая 2016 г., 1:09:42 UTC+3 пользователь Info Cascade написал:
>
> Eugene, 
>
> I recently did this.  The JSON would be something like 
>
> {"location": { "subfield": "some value" }, 
>   "otherfield": "othervalue" 
>  } 
>
> I think I had to use the get_value() or get_attribute() methods because 
> processing the subfield required access to one of the other fields 
> higher up in the JSON hierarchy, so I needed the whole dictionary. 
> However, that part is not included here, for simplicity. 
>
> class LocationField(serializers.Field): 
>     def to_internal_value(self, data): 
>        subfield = None 
>         try: 
>             # pick out the location dict 
>             location = data.get('location') 
>
>             # pick these out first 
>             subfield = location.get('subfield') 
>         except: 
>              ValidationError("oops!") 
>
>         return { 
>                 'subfield': subfield, 
>                 } 
>
>     def get_value(self, dictionary): 
>         """ Get the location dictionary out of the data dictionary, prior 
>             to calling to_internal_value() 
>         """ 
>         return dictionary 
>
>     def get_attribute(self, obj): 
>         return obj 
>
>     def to_representation(self, obj): 
>         subfield = '' 
>         try: 
>             subfield = obj.subfield 
>         except Exception, e: 
>             log.warn('Unrecognized location: %s', e) 
>
>         return { 
>             'subfield':subfield, 
>             } 
>
> class MyModelAbstractSerializer(serializers.Serializer): 
>     location = LocationField(required=False, allow_null=True) 
>
>     def create(self, validated_data): 
>         location = validated_data.pop('location') 
>         validated_data['subfield'] = location['subfield'] 
>
>         instance = MyModel(**validated_data) 
>         instance.save() 
>         return instance 
>
>     def update(self, instance, validated_data): 
>          <get the location data same as for create()> 
>
>
>
> On 05/19/2016 09:14 AM, Eugene Lisitsky wrote: 
> > Hi! 
> > 
> > I have to deal with API with encapsulated data, like this: 
> > 
> > | 
> > { 
> >   "action":"update", 
> >   "issue":{ 
> >          "id":123, 
> >          "Key":"KEY-234", 
> >          "fields":{ 
> >              "creator":"bill", 
> >              "summary":"The issue summary", 
> >              "category":"cool"}, 
> >  "user":"bob", 
> >  "timestamp":3555455 
> > } 
> >   
> > | 
> > 
> > I need to take some fields from `issue` and put the data to some flat 
> > model like: 
> > 
> > | 
> > classIssue(models.Model): 
> >     key =models.CharField(...) 
> >     summary =models.CharField(...) 
> >     category =models.CharField(...) 
> > | 
> > 
> > But here field `key` and field `summary` are located in different json 
> > objects. How can I extract summary during parsing? 
> > 
> > How to write a Serializer? 
> > 
> > I've tried to make something using `.to_internal_value()` and 
> > `.representation()` but it looks ugly. 
> > Also it doesn't work, because it tries to find `summary` field during 
> > validation and fails. 
> > 
> > Have I to write down my own Field class which will be capable of some 
> > specific parsing like looking into nested objects. 
> > How to make a representation for this field if I home some of them in 
> > one serializer? I will have to merge output into one json object. 
> > 
> > Thank you. 
> > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "Django REST framework" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> > an email to [email protected] 
> <javascript:> 
> > <mailto:[email protected] <javascript:>>. 
>
> > For more options, visit https://groups.google.com/d/optout. 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to