Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retrieving model for reverse relations #573

Open
wants to merge 4 commits into
base: 1.21.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import inspect
import itertools
import logging
import operator
import typing
Expand Down Expand Up @@ -212,15 +213,23 @@ def get_related_model(model, source):
:param str source: related field name
:return: related model or ``None``
"""
descriptor = model
try:
descriptor = getattr(model, source)
try:
return descriptor.rel.related_model
except Exception:
return descriptor.field.remote_field.model
except Exception: # pragma: no cover
for attr in source.split('.'):
descriptor = getattr(descriptor, attr)
except AttributeError: # pragma: no cover
return None

try:
is_forward = descriptor.field in itertools.chain(model._meta.fields, model._meta.many_to_many)
except AttributeError: # pragma: no cover
return None

if is_forward:
return descriptor.field.related_model
else:
return descriptor.field.model


class RelatedFieldInspector(FieldInspector):
"""Provides conversions for ``RelatedField``\\ s."""
Expand Down
9 changes: 8 additions & 1 deletion testproj/users/migrations/0002_setup_oauth2_apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Generated by Django 2.1.3 on 2018-12-19 07:57
import pkg_resources
from django.conf import settings
from django.db import migrations

Expand Down Expand Up @@ -27,10 +28,16 @@ def add_oauth_apps(apps, schema_editor):
Application.objects.get_or_create(client_id=app['client_id'], defaults=app)


def get_oauth_dependency():
oauth_version = pkg_resources.get_distribution('django-oauth-toolkit').version.split('.')[:2]
# 1.1 and 1.2 are before squasing migrations into 0001_initial
return '0006_auto_20171214_2232' if oauth_version in (('1', '1'), ('1', '2')) else '0001_initial'


class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('oauth2_provider', '0002_auto_20190406_1805'),
('oauth2_provider', get_oauth_dependency()),
('users', '0001_create_admin_user'),
]

Expand Down
2 changes: 1 addition & 1 deletion tests/test_schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
from django.db import models
from django.urls import path
from django.utils.inspect import get_func_args
from django_fake_model import models as fake_models
from rest_framework import routers, serializers, viewsets
from rest_framework.decorators import api_view
from rest_framework.response import Response

from django_fake_model import models as fake_models
from drf_yasg import codecs, openapi
from drf_yasg.codecs import yaml_sane_load
from drf_yasg.errors import SwaggerGenerationError
Expand Down