How to get querystring value while loading the class in django - python

I need to get the value from querystring which is passing through ajax source. I have class named as xxxx. While loading the class i want to get the value from that querystring. I can able to get the value inside the method using request.GET.get('xxxx') syntax. But i want to get the value while loading the class.
sAjaxSource: "/api/helpdesk/?format=json&xxxx=10",
I have mentioned the ajax url above.
My api.py file:
class helpdesk(ModelResource):
class Meta:
""" Here i would like to get the xxxx value"""
Inside the method i can easily get it using request.GET.get("xxxx").Plz anyone help me to do this.Thanks in advance.

I don't think that's possible.
django-tastypie and similar packages for creating REST APIs leverage the functionality Class-Based Views (CBV) allow, which means that each of those URLs have been assigned the instance method they will use as its view when a request is made to such URL which, in turn, means that the class has been initialized before the request object is passed to any methods of the class.
At that point, the class does not know anything about your requests.

Related

Other methods on class based views in Django

I currently have a class based view with get(), post() and delete() methods for the following URL:
/property/<str:property_uuid>
This works fine and is great.
However, I now want URLs such as /property/<str:property_uuid>/publish and /property/<str:property_uuid>/publish to make my API easier to use. These will be GET methods.
I was hoping to have corresponding publish() and unpublish() functions and put these within my class. However, I'm not sure how to do this or if this is even posible with class based views? Would I have to go back to function based views with all my functions defined in a "property.py" file for ease of reference and then include this in my urls.py file?

Access Django model name from admin url pattern

Given an url like:
http://127.0.0.1:8000/admin/foo/bar/
How can I access the bar variable from the request, using the request.resolver_match.kwargs ?
What kwarg does the admin give too bar?
Tried different keys like class, content_type, slug, model, but none of them work.
EDIT:
It seems the correct kwarg is model_name which is returning None, however. I managed to extract the model name using:
request.resolver_match.url_name.split('_')[1]
But wondering if there is a more direct way.
That should be just the app label. If you're hitting that url, then you are in the ModelAdminclass. To access the app label (foo in the question example) from ModelAdmin you can do self.model._meta.app_label, so you shouldn't need to go through the request.
You can access the model (bar in question the example) in ModelAdmin with self.model.
The model name won't be directly in the request object unless you put it there. You can get it from the request.path.strip('/').split('/')[-1] or from request.resolver_match.url_name.split('_')[1]

Making Django's CBV's get an instance method

I'm currently building a Django app which uses a singleton object.
I want to save this object as a CBV variable because I dont want to initialize it for every 'get' call.
My question in short - can you make a CBV's get function an instance method instead of a classmethod?
And if so, can I save a variable as an instance variable?
EDIT - A better explanation to my question:
I created a class that handles a serialized connection with an electronic measurment instrument.
This class must have only 1 instance (singleton), if another instance will be created a memory leak will crash python.
I want to use it with django in the following way:
Get request to a certain url address ->
The view will ask the instrument class instance for data->
Instance responds with data ->
View returns a JsonResponse with the data.
I think the best way to do it is making the CBV's get method (whose related to the url im getting from) an instance method, but its not such a good practice..
How should I do it?
As I said, get is an instance method.
But you are confusing responsibilities here. A class should have one responsibility only. The view class has the responsibility of responding to the request and returning a response; this is quite separate from the connection to the instrument. That should be a separate class, which is instantiated at module level and used within the view class.

Can we get request parameters from Django settings or other variables?

I see that the request parameters can be obtained in a viewset function using request variable.
eg:
#detail_route(methods=['get'])
def mysampleviewsetfunction(self, request):
print request
But, I want to be able to access request from some common variable. The purpose of that is to write a common function that can be called from all viewsets. This common function should be able to access request parameters depending upon the viewset that called it. But for some reason, I don't want to send the request parameter to this common function.
My code should look like this:
#detail_route(methods=['get'])
def mysampleviewsetfunction(self, request):
commonfunction()
def commonfunction():
print xxxx.request
This 'xxxx' should be some global django variable that stores the current viewset's request. Is there any global variable that stores current viewset's request?
it'd save me the trouble of modifying all the functions.
You wouldn't have to if you provide a default.
def commonfunction(request=None):
This of course means that any existing function calls won't be able to use the request until you update them so you'll need a check to make sure the request isn't none in this function.
There isn't any way you could use a global variable since that won't work asyncronously

Is it possible to call different function of class based views through different actions in template

I am new to django and i ahve gone through all the docs of django. right now if we give some link in template and defined that link in urls.py i.e which view is going to handle that link. like this url(r'^dashboard/gift/$', login_required(CouponPageView.as_view())),
But i have this little doubt can i call different function of a view on clicking different links present in template.
The idea behind a class-based view is not to serve multiple resources (the targets of the links in your template). The idea is that the class-based view implements methods for the various HTTP methods (i.e. get, post, put, delete, head).
So you can server an HTTP GET of a certain URI using the SomeView.get() method, or you can handle a POST to the same resource from the post() method in the same SomeView class. This is helpful to support object oriented code, as the different methods on the object will typically share some resources.
If you want to handle different URL's, write different View classes. If their functionality is similar, use inheritance to prevent code duplication. If their functionality is almost identical, use parameters in the urlpattern.
I think you need to study the URL dispatcher a little more: https://docs.djangoproject.com/en/dev/topics/http/urls/

Categories