I am trying to use Writable Nested Serializer
<http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers>
with
AngularJS. I have image field in the related model. So its something like
this:
class TrackSerializer(serializers.ModelSerializer):
class Meta:
model = Track
fields = ('order', 'title', 'duration', 'thumbnail') # ImageField here
class AlbumSerializer(serializers.ModelSerializer):
tracks = TrackSerializer(many=True)
class Meta:
model = Album
fields = ('album_name', 'artist', 'tracks')
def create(self, validated_data):
tracks_data = validated_data.pop('tracks')
album = Album.objects.create(**validated_data)
for track_data in tracks_data:
Track.objects.create(album=album, **track_data)
return album
Now I want to post the data in the docs its mentioned like this -
>>> data = {
'album_name': 'The Grey Album',
'artist': 'Danger Mouse',
'tracks': [
{'order': 1, 'title': 'Public Service Announcement', 'duration': 245},
{'order': 2, 'title': 'What More Can I Say', 'duration': 264},
{'order': 3, 'title': 'Encore', 'duration': 159},
],}>>> serializer = AlbumSerializer(data=data)>>>
serializer.is_valid()True>>> serializer.save()<Album: Album object>
How to achieve same using AngularJS. I am trying to do the same using
FormData in AngularJS. But its returning the nested object as string list
instead of list.
How to post data using AngularJS forms with imagefield in Nested model. I
am even not able to replicate the above case because the Angular is
returning like this in request.data -
{
'album_name': 'The Grey Album',
'artist': 'Danger Mouse',
'tracks': "[
{'order': 1, 'title': 'Public Service Announcement', 'duration': 245},
{'order': 2, 'title': 'What More Can I Say', 'duration': 264},
{'order': 3, 'title': 'Encore', 'duration': 159},
]", # note that list here is unicode instead of list}
How should this be done ?
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/64de1b15-5e1f-464a-bfaf-ffe3d21993b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.