Created
April 15, 2013 15:04
-
-
Save hughes/5388757 to your computer and use it in GitHub Desktop.
An attempt at limiting fields in a kinda okay way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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