non_field_errors with nested relationships

2,540 views
Skip to first unread message

Sebastian

unread,
Jul 27, 2014, 6:44:53 AM7/27/14
to django-res...@googlegroups.com
Hello everyone,

I've recently started learning and (developing with) the Django REST framework. I tried to use the "nested relationship" within my model and got a "non_field_errors" in the browsable API web view. Then I tried the simple album+tracks example from the documentation, which got the same error.


models.py

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')
        order_by = 'order'

    def __unicode__(self):
        return '%d: %s' % (self.order, self.title)

serializers.py

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 (at http://.../api/albums):
Clicking the OPTIONS button reveals the actual correct data structure:



The raw data input of the browsable browser view shows:

{
    "album_name": "", 
    "artist": "", 
    "tracks": null
}

Posting some raw-data actually works. But it'd be nicer if the web interface would work as well. Especially since I'm wondering if there's something wrong anyway. (even though it seems to work)

Thank you in advance! :)


Regards,

Sebastian

Sebastian

unread,
Jul 27, 2014, 6:47:41 AM7/27/14
to django-res...@googlegroups.com
It didn't post my pictures:

Error:
The "Track" Field is marked red with the message "non_field_errors" attached.

The OPTIONS button just reveals the expected structure.
Message has been deleted

Christopher Lee

unread,
Jul 29, 2014, 9:47:45 AM7/29/14
to django-res...@googlegroups.com
Do you have anything inside of the database already? 

How are you POSTing your data? via the web interface?

Sebastian

unread,
Jul 30, 2014, 7:43:47 PM7/30/14
to django-res...@googlegroups.com
Thanks for the quick reply!

Actually it is working quite nicely. With the only exception of the built in DRF web API interface.

Yes, I added some data to the database with the Django admin interface and later just through the API with httpie.

The only "problem" now is that I can't figure out how to make the API web interface work. Wenn I open the album view it just shows me an (red) input field for the tracks. With the mentioned error message. I'm not even sure if the API browser interface supports such data types?

Thanks for the help! :)

Christopher Lee

unread,
Aug 1, 2014, 9:30:13 AM8/1/14
to django-res...@googlegroups.com

Non_field_errors are errors that are not field related. For example, if a serializer is expecting a list but you input a dict, it will return non_field_error.

Since you set "(many=True)", I think your serializer is expecting a list of dictionaries My guess is that you are just inputting a dict.

Sebastian

unread,
Aug 2, 2014, 11:21:56 AM8/2/14
to django-res...@googlegroups.com
I took the example right from the documentation. So as far as I converted that should be error-free :) and it is actually working. If I use the raw-data-tab (or just curl) and put in a list of dicts it works fine!

Only the browsable web interface doesn't seem to be able to cope with the list of dicts:

It's fair enough if that fails to work but it would be nice if it didn't :)

Thanks again!

Christopher Lee

unread,
Aug 4, 2014, 9:24:31 AM8/4/14
to django-res...@googlegroups.com
If it's the form needs a JSON input, try running your data through a json checker. (namely the u'mmytitle'). It is weird that it works in the raw input and not the form so I don't think this si the case. Also, usually it returns a slightly more descriptive errors. Here are all the non_field_errors in the serializer.py file:

non_field_error/invalid data
non_field_error/no_input_provided
non_field_errors/Cannot create a new item, only existing items may be updated.
non_field_errors/Expected a list of items.

Also I don't know if this matters, but your Tracks field is capitalized. 

Christopher Lee

unread,
Aug 6, 2014, 9:30:50 AM8/6/14
to django-res...@googlegroups.com
Just some digging around. I'm not sure what causes the HTML form to have that error, but when I threw a breakpoint on "def field_from_native" it seems like the self.errors field is already populated with "non_field_error". In "def from_native" (which deserializes primitives to objects), the self.errors also contains "non_field_error" but is reset when using raw data input. When using the HTML form, this function doesn't seem to be called. 
Message has been deleted

Aslhey Ramirez

unread,
Dec 3, 2014, 9:46:00 AM12/3/14
to django-res...@googlegroups.com
Sebastian, could you solve it?
I get this problem to.

Sebastian

unread,
Dec 8, 2014, 3:57:23 AM12/8/14
to django-res...@googlegroups.com
Hi Aslhey (Ashley?),

no, I did not solve it. I just ignored it, since everything apart from the browsable API worked well. And I didn't really need the browsable API.

But: I have yet try this again with the current version of Django and DRF, since the problem occured with Dj1.6 and DRF2.x
If I get to try it, I'll post my results :)

Cheers!
Reply all
Reply to author
Forward
0 new messages