From e80c22bce4086faa592d6536413c369dbc4dc17f Mon Sep 17 00:00:00 2001 From: John Date: Wed, 31 Jan 2018 00:39:24 +0800 Subject: [PATCH] AttributeError: 'NoneType' object has no attribute '_meta' I follow the official guide to extend the default User model, so that user can login with email instead of username. I checked my code and it is same as (at least two or three) online tutorials, including the official one. When I build a basic serializer as other models, and tried to list all users, I got `AttributeError: 'NoneType' object has no attribute '_meta'` error: File "/rest_framework/utils/model_meta.py", line 96, in _get_forward_relationships not field.remote_field.through._meta.auto_created Where: field = auth.User.groups field.remote_field.model = Referring to the function `def _get_reverse_relationships(opts):` below, which guard with None check, I added this None check in `_get_forward_relationships` and the serializer works. --- rest_framework/utils/model_meta.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rest_framework/utils/model_meta.py b/rest_framework/utils/model_meta.py index 4cc93b8ef..5a96bb553 100644 --- a/rest_framework/utils/model_meta.py +++ b/rest_framework/utils/model_meta.py @@ -93,6 +93,7 @@ def _get_forward_relationships(opts): # manytomany do not have to_fields to_field=None, has_through_model=( + (getattr(field.remote_field, 'through', None) is not None) and not field.remote_field.through._meta.auto_created ), reverse=False