Making Django's CBV's get an instance method - python

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.

Related

How does Django work with either instance id and instance itself?

For instance django queryset's filter takes either instance or instance_id.
Probably django code needs id for Database query, and it extracts instance_id when it receives instance
Although I don't remember, there could be a scenario where django would want instance but receives instance_id .
How does django handle such cases? just do typechecking with isinstance(something, models.Model) and do something with it?

Initiation call Django class based view.

This is more of a conceptual question. While learning the Django class-based view, I am wondering if it is possible to make a call to a Django view as an initiation call. I mean, after the first call, the following calls from the templates can share the instance variables created by the first one. This avoids passing variables back and forth between the template and server.
No. Django views are specifically designed to prevent this. It would be a very bad idea; any instance variables set would be shared by all future users of that process, leading to potential information leakage and other thread-safety bugs.
If you want to store information between requests, use the session.

How can I create a form instance outside of a view?

I'm currently writing a Flask app. One of my views has very complex business logic so I moved that to a class declared outside the view. In the constructor of that class I create several instances of flask_wtf.form.Form objects.
My problem is that at runtime I get the following error:
*** RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in a way. To solve
this set up an application context with app.app_context(). See the
documentation for more information.
(ipdb is mine)
I assume the form objects need to be in the view? But I want to move the work of creating them into a separate class so the view won't get too complex, otherwise it's unmanageable.
You can't. flask_wtf.Form requires the application context to set up CSRF.
It doesn't really make sense to instantiate a form outside of where it will be used, because you need to instantiate it with the data that is submitted to do anything useful.
Move creating the form instances to a method that you call on that class, rather than in it's __init__ method.

How to get querystring value while loading the class in django

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.

How to customize Django inline admin forms based on the object instance

The Django documentation makes it clear how to customize ModelForm instances based on the attributes of the particular Model instance being edited. However, I am working with a design that involves a lot of foreign key relationships between models, and I keep running into situations where I would like to modify a particular inline form instance based on the inline Model associated with it. I have dug through the documentation and the code, but the solution for this is eluding me.
The closest thing to a hook for this that I've been able to find is InlineModelAdmin.get_formset(), but the object instance that gets passed to that method is the parent object, not an instance of the child object. My instinct is that there is a way to do this, though. Does anybody know the proper way?
I am not 100% sure I fully understand what you are asking, but you can specify a forms.ModelForm for the admin inline (https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin.form) and that receives an instance of the current inline object and then you can change the form fields based on the instance.

Categories