I think dehydrate
was made for cases like yours. Add query set argument to determine that dehydrate should compute addition fields or not.
def dehydrate(self, bundle):
if bundle.request.GET.get('attach_dynamic_fields'):
bundle.data['my_dynamic_field_1'] = make_dynamic_field(1)
bundle.data['my_dynamic_field_2'] = make_dynamic_field(2)
return bundle
Then just use when needed:
/api/v1/complex/ # Gives all, no dynamic fields attached
/api/v1/complex/1/ # Gives one, no dynamic fields attached
/api/v1/complex/?attach_dynamic_fields=1 # Gives all, with dynamic fields
/api/v1/complex/1/?attach_dynamic_fields=1 # Gives one, with dynamic fields
Creating prepend_url
doesn't make sense for me. Because it means rewriting Tastypie again. Which is sign that something here is not RESTful.
If you want to do it anyway you should take a look on dispatch
method and its flow:
Flow through request / response cycle
You will have to rewrite this: dispatch method in your prepend_url
somehow.