concentricsky / django-tastypie-swagger

An adapter to use swagger-ui with django-tastypie.
Other
132 stars 144 forks source link

Fetching ModelResource build from inherited models fails #88

Open pleveau opened 10 years ago

pleveau commented 10 years ago

Hello, I found this issue when trying tastypie swagger on some Resources created from inherited models. This may be more general and related to the handling of OneToOne fields...

Here is the code:

from django.db import models
from tastypie.resources import  ModelResource

class SampleModel(models.Model):

    pass

class InheritedSampleModel(SampleModel):

    pass

class SampleResource(ModelResource):
    class Meta():
        queryset = SampleModel.objects.all()
        resource_name = "sample"

    pass

class InheritedSampleResource(ModelResource):
    class Meta():
        queryset = InheritedSampleModel.objects.all()
        resource_name = "inheritedsample"
    pass

and the traceback:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/api/v1/doc/schema/inheritedsample/?api_key=special-key

Django Version: 1.5.8
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'tastypie_swagger')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Traceback:
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  113.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/django/views/generic/base.py" in get
  153.         context = self.get_context_data(**kwargs)
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/tastypie_swagger/views.py" in get_context_data
  125.         mapping = ResourceSwaggerMapping(resource)
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/tastypie_swagger/mapping.py" in __init__
  71.         self.resource_pk_type = self.get_pk_type()
File "/home/pleveau/virtualenvs/vega-app/local/lib/python2.7/site-packages/tastypie_swagger/mapping.py" in get_pk_type
  77.             return DJANGO_FIELD_TYPE.get(self.resource._meta.object_class._meta.pk.related_field, 'unknown')

Exception Type: AttributeError at /api/v1/doc/schema/inheritedsample/
Exception Value: 'OneToOneField' object has no attribute 'related_field'

Using tastypie v 0.11.1 and tastypie-swagger master (sha 3d3b2b42a867fe60b2b204e06142e5164fe9e0b6) . Resource that are related to parent models (SampleModel in the example) are working fine.

Thanks for all the good work!

johnraz commented 10 years ago

Hey, thanks for the report. It seems that we need to handle OneToOneField with special care... I can't remember on the top of my head where the related_field could be on such a field but it is probably defined somewhere else.

Do you mind giving it a go and try to fix it ? That would be awesome as I don't have much time to spend writing code for this project nowadays.

Thx !

fluffels commented 10 years ago

Hi, I'm looking into this.

Something seems to be wrong, though. From your stack trace, the error seems to originate from tastypie_swagger/mapping.py:77. That line does not seem to correspond to the return statement at all, either in the ref you quoted, or in the current master branch.

Worse, I can't find code that even looks like that in either of these refs. I can find the line self.resource_pk_type = self.get_pk_type(), but that references a function that doesn't look like the one in the final line of that stack trace.

Am I missing something?