See the class below. If I leave out the hydrate() method, the tastypie seems to ignore my excludes list. I create a new object (by doing a POST on .../user/) and get back a 201, which is what I expect. But, the data in the body includes my excluded fields (until I added the dehydrate() method). Am I just doing something wrong in my obj_create() that's causing the excludes to be ignored?
class UserResource(MongoEngineResource):
class Meta:
authorization = authorization.Authorization()
queryset = User.objects.all()
resource_name = 'user'
object_class = User
always_return_data = True
excludes = ["utm_source",
"utm_medium",
"utm_referrer",
"utm_campaign",
"email",
"lc_email",
"lc_username",
"password",
"status",
"site",
"password_recovery_code",
"email_validation_code",
]
read_only_fields = ['user_id']
def __init__(self):
super(UserResource, self).__init__(self)
for f in getattr(self.Meta, 'read_only_fields', []):
self.fields[f].readonly = True
# Unclear why this is needed. Shouldn't the default implementation
# handle deleting all the excluded fields?
def dehydrate(self, bundle):
logger.debug("original bundle.data = %s", bundle.data)
for f in getattr(self.Meta, 'excludes', []):
if f in bundle.data:
del bundle.data[f]
logger.debug("final bundle.data = %s", bundle.data)
return bundle
def obj_create(self, bundle, request=None, **kwargs):
logger.debug("bundle.data = %s", bundle.data)
try:
bundle.obj = User.create(bundle.data.get('username'),
bundle.data.get('email'),
'songza',
'web',
'US',
bundle.data.get('password'))
except User.CreateError as ex:
raise exceptions.ValidationError(ex.message)
logger.debug("new user = %r", bundle.obj)
return bundle