Reputation: 185
class Hello(models.Model):
name = models.CharField(max_length=8, blank=True)
column_create_no_update = models.CharField(max_length=8, blank=True)
class HelloResource(ModelResource):
def dehydrate(self, bundle):
if (bundle.request.META['REQUEST_METHOD'] == 'PUT') and ('column_create_no_update' in bundle.data.keys()):
del bundle.data['column_create_no_update']
return bundle
1) Create a record
createData['name'] = 'foo name';
createData['column_create_no_update'] = "don't update me";
Ajax POST creates a record in db.
2) When updating the table with ajax call,
updateData['name'] = 'foo name updated';
Ajax PUT update the record. No 'column_create_no_update' is provided in update.
I noticed in function dehydrate(), bundle.data['column_create_no_update'] = '' and bundle.data['column_create_no_update'] is deleted. only bundle.data['name'] exists after delete when 'bundle' is returned.
But, in database, 'column_create_no_update' is updated with ''. I want it to be preserved: column_create_no_update = "don't update me".
Why it's updated with null string ''?
Upvotes: 0
Views: 171
Reputation: 316
You can override update_in_place method in your class. Main function of this method is to update old data with the new data only for PUT requests so you dont need to add additional check for request method. You can find tastypie code here https://github.com/django-tastypie/django-tastypie/blob/6721e373de802648ce0fad61d15c07fc01422182/tastypie/resources.py#L1714
class Hello(models.Model):
name = models.CharField(max_length=8, blank=True)
column_create_no_update = models.CharField(max_length=8, blank=True)
class HelloResource(ModelResource):
def update_in_place(self, request, original_bundle, new_data):
if ('column_create_no_update' in new_data.keys()):
del new_data['column_create_no_update']
return super(HelloResource, self).update_in_place(request, original_bundle, new_data)
Upvotes: 1