In the Django-REST-framework project, I tried using a nested relationship and got "non_field_errors" in the web API being viewed.
Code from this part of the documentation: http://www.django-rest-framework.org/api-guide/relations#nested-relationships
models.py:
from django.db import models class Album(models.Model): album_name = models.CharField(max_length=100) artist = models.CharField(max_length=100) class Track(models.Model): album = models.ForeignKey(Album, related_name='tracks') order = models.IntegerField() title = models.CharField(max_length=100)
serializers.py:
from rest_framework import serializers from myapp.models import Album, Track class TrackSerializer(serializers.ModelSerializer): class Meta: model = Track fields = ('order', 'title') class AlbumSerializer(serializers.ModelSerializer): tracks = TrackSerializer(many=True) class Meta: model = Album fields = ('album_name', 'artist', 'tracks')
ERROR (on ../ albums):
The input field for the track is marked in red with the error message: non_field_errors.
Pressing the OPTIONS button shows the actual and correct data structure:
Tracks nested in the corresponding property
The raw data entry of the browser being browsed shows:
{ "album_name": "", "artist": "", "tracks": null }
Actually posting some valid source data. But it would be better if the web interface form worked too. Especially since Iβm wondering if something funny happens anyway.
Thank you in advance!
source share