Growing my comments into a more detailed case. Your views will usually look like this:
def my_view(request):
data = deserialize_data_from_request(request)
result = do_something_with_data(data)
return serialize_result_into_response(result)
What you want, I believe, is to do_something_with_data from outside your project directory. You have two ways to achieve this, without repeating yourself:
1) The hackish way.
You are not handling an HTTP request, so you:
- fire up Django testing tools
- create a request mockup
- serialize your data into the request mockup
- call your view on the request mockup
- your view
deserializes_data_from_request
- your view
does_something_with_data
- your view
serializes_result_into_response
- extract the serialized result from the response
- deserialize the result
Although it works, it is definitely kludgy and convoluted. The only reason to do this is when building view testcases, since then you actually want to test the deserialization/serialization processes.
See Sebastian Wozny's answer to do this.
2) The clean way.
Extract the task from the view. Change your view module in this way:
def my_save_function(data):
return do_something_with_data(data)
def my_view(request):
data = deserialize_data_from_request(request)
result = my_save_function(data)
return serialize_result_into_response(result)
Now, from your external code, you can directly invoke my_save_function. No need to load Django testing facility. No need to serialize/deserialize your data several times.
do_save_tasks. You can then use this function from both your external code and your view. That way it's DRY and you do not misuse Django testing facility.