Django REST Structure: Nested Relationships: non_field_errors

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) #duration = models.IntegerField() class Meta: unique_together = ('album', 'order') ordering = ('order',) def __unicode__(self): return '%d: %s' % (self.order, self.title) 

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!

+6
source share
2 answers

I also ran into this problem. One way to get rid of the error is to use:

 class AlbumSerializer(serializers.ModelSerializer): tracks = serializers.RelatedField(many=True) class Meta: model = Album fields = ('album_name', 'artist', 'tracks') 

However, this removes the nested fields of the tracks and displays only a string representation of the tracks.

Edit: I figured this out. What you want is:

 class AlbumSerializer(serializers.ModelSerializer): class Meta: model = Album fields = ('album_name', 'artist', 'tracks') read_only_fields = ('tracks',) depth = 1 

This will cause the tracks to be nested without throwing a user interface error.

+3
source

One solution is to simply hide the HTML form on the browser side. For example, override the Rest Framework api.html template (by creating your_app / templates / rest_framework / api.html) and include the following:

{% extends "rest_framework/base.html" %}

...

 {% block script %} {{ block.super }} <script> $('.form-switcher a[name="html-tab"]').hide(); $('.form-switcher a[name="raw-tab"]').tab('show') </script> {% endblock %} 

If you want to save an HTML form for your endpoints and simply remove it from your nested ones, you can use the name variable as an indicator. For example, include β€œNested” in the names of your nested endpoints and do something like this:

 if("{{ name }}".indexOf("Nested") >= 0){ $('.form-switcher a[name="html-tab"]').hide(); $('.form-switcher a[name="raw-tab"]').tab('show').hide(); } 
0
source

Source: https://habr.com/ru/post/972917/


All Articles