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

Fetching ModelResource build from inherited models fails #88

Open
pleveau opened this issue Aug 14, 2014 · 2 comments
Open

Fetching ModelResource build from inherited models fails #88

pleveau opened this issue Aug 14, 2014 · 2 comments

Comments

@pleveau
Copy link

pleveau commented Aug 14, 2014

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 3d3b2b4) . Resource that are related to parent models (SampleModel in the example) are working fine.

Thanks for all the good work!

@johnraz
Copy link
Contributor

johnraz commented Sep 8, 2014

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
Copy link
Contributor

fluffels commented Sep 8, 2014

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants