I have a Tastypie ModelResource which gets its fields from a regular Django Model. I would like to make certain fields read-only on the Tastypie resource, even though they are writeable in the underlying model. Is this possible to accomplish in a simple way?
I've tried the following to no avail:
def __init__(self, **kwargs):
super(ModelResource, self).__init__(**kwargs)
for f in getattr(self.Meta, 'read_onlys', []):
self.fields[f].read_only = True
Normally I would do that sort of thing in the hydrate/dehydrate process.
There are probably other ways,
def hydrate(self, bundle):
if bundle.obj.pk:
bundle.data['somefield'] = bundle.obj.somefield
else:
bundle.data.pop('somefield', None) # no KeyError if 'somefield' missing
return super(MyResource, self).hydrate(bundle)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With