Dynamic Models in tastypie

霸气de小男生 提交于 2019-12-11 12:58:59

问题


I have a class which returns json

[{      
            'title': 'Test Blog Title 1',
            'content': 'Blog Content',
            'author_name': 'User 1'
 },
 {
            'title': 'Test Blog Title 2',
            'content': 'Blog Content 2',
            'author_name': 'User 2'
 }]

I want to create Tastypie Model Resource based on the returned Json

I tried the Below Url this worked but i dont want to declare fields it should be dynamic

http://thehungrycoder.com/python/using-non-orm-data-sources-with-tastypie-in-django.html

class BlogResource(Resource):
    #i dont want the fields below instead want it to be dynamic based on json
    title = fields.CharField(attribute='title')
    content = fields.CharField(attribute='content')
    author = fields.CharField(attribute='author_name')

    class Meta:
        resource_name = 'blogs'

回答1:


If you' don't declare fields, they won't be accessible in the bundle. However, they'll always be accessible in the request.

You'll need to have at least one predefined field, to be used as a primary key. While you don't explicitly need to create it, you should have a way of knowing what object to return if the user issues a GET request for some object.



来源:https://stackoverflow.com/questions/15739287/dynamic-models-in-tastypie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!