Comments (9)
Interesting...
Having the create a mock request to use the views internally is pretty icky.
I've been planning to rewire how the model mixins and model views stick together, and I think it'd also solve the problem of how to access views internally. Would be great to get your feedback.
So the various model mixins, rather than defining the .get(), .put(), .post() method handlers directly, would instead define the .read(), .update(), .create() actions. Eg: http://pastebin.com/8bFQJGwc (It'd also be possible to then move them all into a single ModelMixin class for the sake of brevity)
The model views would then define the .get(), .put(), .post() method handlers, mapping them to the correct actions. Eg: http://pastebin.com/pv0qMaEy. The model views would be the place that self.CONTENT gets accessed if required, and handle any request/response specific stuff (eg returning redirects to created model instances)
In which case your code would look like:
@render_to('monitoring_added.html')
def add_monitoring(request, entity_id):
response = MonitoringApi().create({'entity_id': ..., 'user_id': ...}
return {'content': response.content}
(Or possibly...)
@render_to('monitoring_added.html')
def add_monitoring(request, entity_id):
api = MonitoringApi()
response = api.fiter_response(api.create({'entity_id': ..., 'user_id': ...}))
return {'content': response.content}
Does this make sense, and does it solve what you're trying to do more nicely?
from django-rest-framework.
Hm, this could be a possibility. But there could be a problem with the authentication. In some cases, the authentication needs the csrf token, which is contained in the request. By passing a request object on to a "fake request handler", there should be no problem. But if the create/update methods accept the direct values instead of the request objects, authentication would be circumvented and would have to be duplicated.
Or maybe I didn't fully understand your answer :)
from django-rest-framework.
But still, I think the approach using a request and modifying it should work in theory. Do you know what the problem could be with the code in the issue description?
from django-rest-framework.
Yeah, so you're modifying request.POST, which isn't actually the underlying request content.
(Rather a property which reads from the stream, parses it, stores the result and then returns it.)
If you were instead modifying request._stream (or .stream I don't remember right now), and setting it to the url-encodeded/multipart encoded form data then everything would work fine - REST framework's FormParser/MultiPartParser would read the stream and decode it into the form dictionary. As it is they'll look at the stream and find it empty.
It'd be possible to modify REST framework to deal with your use case, but there's a bit of fiddly-ness involved.
Eg at the moment FormParser and MultiPartParser handle parsing arbitrary streams.
from django-rest-framework.
Ok, thanks for your answer. I think that would be a great feature, but for now I'll just send a HTTP request to my own server (even though that's pretty messy :)).
from django-rest-framework.
The easiest hack way to make it work would prob be to use a custom FormParser and MultiPartParser on each request that subclass the existing parsers, but override the .parse() method like so:
class CustomFormParser(FormParser)
def parse(self, stream):
return (self.view.request.POST, self.view.request.FILES)
from django-rest-framework.
Note that this is now being worked on, in the experimental branch...
https://groups.google.com/forum/#!topic/django-rest-framework/JJJH28NoPSE
from django-rest-framework.
Thanks for the note.
from django-rest-framework.
Example using REST framework 2.0.
(No idea if this'd work fine in 0.4, but I suppose so.)
from django-rest-framework.
Related Issues (20)
- 3.15.0 - bug in rendering `%` characters from `ValidationError` HOT 4
- `permissions.DjangoModelPermissionsOrAnonReadOnly` doesn't actually enable anonymous read-only access in 3.15 HOT 3
- Error: An admin for model "User" has to be registered to be referenced by TokenAdmin.autocomplete_fields. HOT 6
- 3.15 not backwards compatible with 3.14 - "View' should either include a `queryset` attribute, or override the `get_queryset()` method." HOT 11
- 3.15 backward compatibility issue with 3.14 - `rest_framework.filters.SearchFilter.get_search_terms` returns `str` instead of `list` HOT 3
- New handling of default= for ModelSerializer HOT 6
- 3.15 regression: ListSerializer ValidationErrors silently changed return type
- 3.15 regression: ListSerializer ValidationError nested structure silently changed HOT 1
- 3.15 regression: UpdateModelMixin breaks views using Manager objects as queryset HOT 4
- Version 3.15.1 HOT 1
- 3.15 regression: Unset default namespace version suddenly raises 404 HOT 3
- 3.15(.1?) regression: optional fields in serializers are suddenly required (or need explicit None passed) HOT 11
- UniqueConstraint violation_error_message as error response in drf
- rest-framework Supports async class views ?
- 3.15 regression: Serializer validation failed for unique together constraint HOT 1
- Revert changes to `CursorPagination` that caused serious performance regression HOT 1
- Router.register cannot merge with urlpatters HOT 3
- UniqueTogetherValidator does not comply to Database standards
- HyperlinkedModelSerializer doesn't respect SECURE_PROXY_SSL_HEADER settings
- 3.15 is raising required error on model nullable fields HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-framework.