Skip to content

Instantly share code, notes, and snippets.

@hughes
Created April 15, 2013 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughes/5388757 to your computer and use it in GitHub Desktop.
Save hughes/5388757 to your computer and use it in GitHub Desktop.
An attempt at limiting fields in a kinda okay way
class MyBaseResource(ModelResource):
# a resource class that dynamically limits fields
def full_dehydrate(self, bundle):
# Dehydrate each field.
for field_name, field_object in self.limit_fields(bundle):
# ...
# ...
def limit_fields(self, bundle):
return self.fields.items()
class ImplementedResource(MyBaseClassResource):
post_only = fields.CharField('attr')
# limits fields according to type of request
def limit_fields(self, bundle):
fields = super(QuestionResource, self).limit_fields(bundle)
exclude = []
if bundle.request.method.lower() != 'post':
exclude.append('post_only')
return [(name, obj) for name, obj in fields if name not in exclude]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment