python - Django Rest Framework list of dict field -
i'm trying serialize 2 model fields provided list of dicts using django rest framework.
this model.py:
class translation(models.model): translated_term = models.charfield(max_length=200) translated_meaning = models.textfield() language = models.charfield(max_length=200) vocab = models.foreignkey(vocab, related_name='translates') def __unicode__(self): return '%s: %s' % ( self.language,self.translated_term)
view.py:
class vocabviewset(viewsets.modelviewset): queryset = vocab.objects.all() serializer_class = vocabserializer
serializer.py:
class vocabserializer(serializers.modelserializer): ... translates= serializers.stringrelatedfield(many=true) ... class meta: model = vocab fields = (...,'translates',...)
api result :
{ ..... "translates": [ "en: abyssal zone" ], .... }
my question how set translates list of dicts this:
expected api result:
{ ..... "translates": [ {en: "abyssal zone"} ], .... }
i'm following docs don't know if write way. tried use serializers.dictfields this:
class translatefield(serializers.dictfield): child = serializers.charfield() class vocabserializer(serializers.modelserializer): term_word = serializers.charfield(source='term.word',read_only=true) kwdgroup = serializers.stringrelatedfield() translates = serializers.dictfield(child=serializers.charfield()) class meta: model = vocab fields = ('uuid','id','term_word', 'meaning', 'source', 'translates','kwdgroup')
it occurs:
'relatedmanager' object has no attribute 'items'
i'm having troubles find approach overcome issue. can please give me opinion issue? suggestion great.
thanks.
Comments
Post a Comment