Dynamically create Resources for multiple django models in tastypie

I need to expose multiple existing django models to tastypie. I got the basics covered with creating af ModelResource and registering it in urls.py. However, I would like to avoid writing Resource classes for every django model and since they all need to work the same way, I would like to have it generalised in some way.

So basically what I hope to archive is with a set of regular django models:

class ModelA:
  field1 = ...
  field2 = ...

class ModelB:
  field3 = ...
  field4 = ...

class ModelC:
  field8 = ...
  field9 = ...

And then automatically have them exposed to tastypie API as '/api/v1/modela/', '/api/v1/modelb/' and '/api/v1/modelc/ and so forth.

Not looking for a complete solution, just suggestions for a good approach to this.


A Resource is 'just' a python class so you could simply create a base resource and inherit it with only the queryset and resource_name Meta attributes defined.

You could probably automate naming too by fiddling with the Resource class's __new__ method or create a custom classmethod, but I'm not sure the effort will gain you much.

Registering the classes to the api can be automated in numerous ways, of which one could be:

for name, obj in inspect.getmembers(sys.modules['resources']):
    if inspect.isclass(obj):  # might want to add a few exclusions

where 'resources' is the name of the module containing resources, but this is kind of implicit..

Need Your Help

CreateFileMapping fails with hFile other than INVALID_HANDLE_VALUE

delphi winapi delphi-7

update - solved and answered, offending lines have been commented out

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.