Last active
December 28, 2015 09:29
-
-
Save AndrewJHart/7478897 to your computer and use it in GitHub Desktop.
Override django-tastypie hydrate_FIELD to remove/delete JSON field(s) (related field in this instance) for tastypie resource on PUT request only before its converted to object and saved.
This file contains hidden or 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
def hydrate_device(self, bundle): | |
""" | |
override hydrate method for incoming bundle hydrate is done on a per field basis (really cool) | |
so this allows to override only the one (related field) and to strip the JSON data from the PUT | |
request thus allowing PUT request to go through successfully. NOTE: Only for PUT requests - we need | |
this data for POST or else the related field (device) wont be created | |
Issue stems from the relation - DeviceSettings has one-to-one to Device, DeviceSettings stores meta | |
data and other stuff about the device w/o interferring with the original device model and allowing | |
separation of concerns to make the Device and Service models work as standalone app for apns/gcm. Thus, | |
unique foreign key constraint on the Device model w/ APNService FK & due to one way relationship | |
defined in this resource or a lack of a Service resource representation at all, raises an issue. | |
Ergo, since backbone keeps a full repr of the model resource & we always return data = True to | |
keep client models in sync and provide them resource URI from response (not to mention long wait time on | |
store propagation for patching the client portion of the app) the alternatives were: | |
1. Upon client registration POST to Device Resource, then post to DeviceSettings resource | |
-- Cons are extra requests and fact that they're related so posting to settings resource already | |
creates the device anyway (due to default value set on device model FK for Service) | |
2. Use PATCH for parial updates (preferred but most servers dont support or require special headers) | |
-- flaky but preferred solution | |
3. Catch the data coming in and pluck out the device field before the data is deserialized and saved | |
-- current workaround hack but nice to know how fluid tastypie is | |
""" | |
if bundle.request.META.get('REQUEST_METHOD') == 'PUT': | |
if 'device' in bundle.data and bundle.data.get('device') != '': | |
del bundle.data['device'] | |
return bundle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment