I've been trying to implement a dashapp into my django project, and I'm doing so by following the demonstration of the official documentation of django-plotly-dash.
Weirdly enough my Dashapp does not get registered, the queryset in
def get_object_or_404(klass, *args, **kwargs): which is in django shortcuts.py is returned empty, whereas in the working example it is a list of Dashapp objects registered.
Any help is much appreciated.
Related
Following this tutorial, I'm trying to add a url to which one could make a POST request without a model:
router.register(r'send_message', SendMessageViewSet, base_name='send_message')
I don't need a GET, but I added one for debugging purposes:
class SendMessageViewSet(ViewSet):
def get(self, request, *args, **kwargs):
return Response(HTTP_200_OK)
def create(self, request, *args, **kwargs):
...
Yet I'm able to get the "list" (url with no pk) but not the specific resource.
Thanks!
You probably have missed my link to the full repository.
You are doomed because you are overriding get instead of list.
See https://github.com/linovia/drf-demo/blob/master/drf_demo/model_less/views.py for a full working example.
In the documentation for dynamic extension of DRF they describe the possibility to to add query parameters through the viewset.(instructions)
These added query parameters act as if they were being passed in the url request, thereby triggering dynamic actions available through the extension.
But I can't get this to work. It seems impossible to make the serializer or router to recognize the alteration of the request instance.
Any suggestions as to where to learn more about how this works, or alternative ways to do it would be greatly appreciated.
class EventViewSet(DynamicModelViewSet):
# …
def list(self, request, *args, **kwargs):
# sideload location by default
request.query_params.add('include[]', 'location.')
# filter for status=current by default
status = request.query_params.get('filter{status}')
if not status:
request.query_params.add('filter{status}','current')
return super(EventViewSet, self).list(request, *args, **kwargs)
I am learning DRF and I have come to the point of View creation using mixins and GenericView. What I want to understand is, In what order are the methods executed?
Lets say I am creating a view like this.
class MyView(mixins.CreateModelMixin, generics.ListCreateAPIView):
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self...):
....(some code)
So, for a POST request which method is executed first and why? When I actually tried this the perform_create got executed first, And I want to understand why POST did not get executed first.
I am sure I missing something here, And I am looking for some document on this if possible.
Viewset's post method just calls a CreateMixin's create method, which gets a data from request, from HTTP POST method, and then serializes and validates the data, and then calls perform_create, which actually creates the model object and writes it to database.
You can see it here:
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py
and here:
https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/viewsets.py
I am using Django Rest Swagger to document my API which is made with Django Rest Framework.
My APIClass has two methods: get and post. And I want the documentation to note this difference but it seems that it expect both to have the same params. Look:
This is generating documentation well:
class Product(APIView):
"""
Do something with product
param1 -- Product name
param2 -- Category
"""
def get(self, request, format=None):
...
def post(self, request, format=None):
...
But what I want is this, which is not generating documentation well:
class Product(APIView):
def get(self, request, format=None):
"""
Get products
param1 -- Parameter for filtering by category
"""
...
def post(self, request, format=None):
"""
Create a new product
param1 -- Product name
param2 -- Category
"""
...
How can I do it? Any idea?? Thanks so much! ;-)
Josep your updated method documentation is correct. The issue you report is well-known in the django-rest-swagger community. See this commit for the fix. Unfortunately Mr. Gibbons has not merged this fix into master, but you can certainly fork and patch as many other people have done.
I'd like to send an email once a POST has been successfully completed. It seems to make sense that the email would be done with a signal. I can write my own, as documented here: https://docs.djangoproject.com/en/1.6/topics/signals/#defining-and-sending-signals
However, what I can't figure out is:
Does Django Rest Framework support signals (why wouldn't it, it's just Django)
What do I listen to to send the custom signal? What is the sender?
Suggestions welcome.
Also, is anyone doing this with Angular? Is there a simple way to do this that I'm not seeing?
Old question for an old version of Django, but if anyone comes across it... It's not necessary to use signals. Django now has the built-in function send_mail. Here's the documentation:
https://docs.djangoproject.com/en/1.8/topics/email/
So you would use that function to create and send an email. It can even accept an HTML template as the message argument and populate it w/ Django data, which is nice.
So you'd define a function that included this method, for example:
def email_client(request):
id = request.POST.get('id')
client = Client.objects.get(id=id)
msg_html = render_to_string('templates/email.html', {'client': client})
template_email_text = ''
return send_mail('Lelander work samples', template_email_text, 'test#emailsender.com', ['test#emailrecipient.com'], html_message=msg_html, fail_silently=False)
And include that function in your DRF API view in post:
class ClientList(generics.ListCreateAPIView):
queryset = Client.objects.all()
serializer_class = ClientSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
email_client(request)
return self.create(request, *args, **kwargs)
Yes, DRF supports signals.
I don't have a good answer for question 2, mainly because I think the best place to do this is in the post_save method of your class. That way, you are sure your object was created, you have access to the object and to the request and you don't need to bother implementing signals (which would probably require another class just for email sending).