wlanslovenija / django-tastypie-mongoengine

MongoEngine support for django-tastypie.
Other
73 stars 59 forks source link

Polymorphic schemas #56

Closed eelkeh closed 11 years ago

eelkeh commented 11 years ago

When my resource class is polymorphic, the /schema url fails, something like this:

class OtherArticle(MongoEngineResource):
    class Meta:
        queryset = models.OtherArticle.objects.all()

     publication = fields.ReferenceField(to='datahub.api.PublicationResource',
                                    attribute='publication', full=True, null=True)

class Article(MongoEngineResource):
    class Meta:
        queryset = models.Article.objects.all()
        polymorphic = {
            'article': 'self',
            'other': OtherArticle,
        }
     publication = fields.ReferenceField(to='datahub.api.PublicationResource',
                                    attribute='publication', full=True, null=True)    

The schema for the article resource returns:

Reverse for 'api_get_schema' with arguments '()' and keyword arguments '{'api_name': None, 'resource_name': 'publications'}' not found

When I remove the polymorphic attribute, it works as expected.

mitar commented 11 years ago

Because we have tests for this I am not sure if this really does not work. Probably tastypie or mongoengine again upgraded their versions and break something. Can you try tests and if they work for you? If they do, then please augment them with another test case which fails. If test fail, try older versions of tastypie or mongoengine until they work. And then try your example again.

mitar commented 11 years ago

I checked and tests work for me. So please make a pull request with a new test which fails.

mitar commented 11 years ago

(I just pushed new version to fix one other tests issue.)

eelkeh commented 11 years ago

Thanks a lot for the quick responses, this can be closed. My issue was that I didn't register my urls on a TastyPie Api instance, this is apparently necessary when generating schema's. Before I did something like this:

article_resource = api.ArticleResource()
...

and include article_resource.urls, but in order for schema's to work properly, you need an Api instance.

v1_api = Api(api_name='1')
v1_api.register(api.ArticleResource())

and include those urls, v1_api.urls

mitar commented 11 years ago

Of course. :-) Only registered resources are accessible through URLs.

eelkeh commented 11 years ago

Well, actually this works fine (apart from schema generation):

article_resource = api.ArticleResource()
urlpatterns = patterns(r'^api/', include(article_resource.urls))
mitar commented 11 years ago

Ehm. And where have you found this documented as supported/official approach? Of course you can find ways things work if you manually extract variables from objects, but then don't complain when things don't work. :-) Have you read Tastypie documentation? Do they have such example there?

eelkeh commented 11 years ago

It's literally lifted from an example in the Tastypie documentation: http://django-tastypie.readthedocs.org/en/latest/tutorial.html#hooking-up-the-resource-s

mitar commented 11 years ago

Interesting. Does schema work in pure Tastypie when it is registered like that?

eelkeh commented 11 years ago

Not sure, haven't looked into it extensively, but in tastypie-mongoengine it seems to break on ReferenceFields when it assumes an api_name property somewhere, as in: Reverse for 'api_get_schema' with arguments '()' and keyword arguments '{'api_name': None, 'resource_name': 'publications'}' not found Not sure if this is worth chasing down though, let me know if I can be of any help though, thanks!

mitar commented 11 years ago

Can you please check what happens with Tastypie when schema has a reference field there?

eelkeh commented 11 years ago

I'll have a look next week!