0

It is possible to do this some how:

class Login_vw(FormView):
    form_class = Login_fr
    success_url = '/success/'
    template_name = 'login.html'

    someVar_ses = self.request.session['someVar']
    record_qr = MyModel.objects.filter(id=someVar_ses)

When I do that I get this:

NameError: name 'self' is not defined

But when I use it within a method like get_context_data it works right.

What I need is to use the record_qr var in different methods and don't repeat the code in each one.

Is there a way?

Edited to add this details:

What I'm trying to do is something like this:

class Login_vw(FormView):
    form_class = Login_fr
    success_url = '/success/'
    template_name = 'login.html'

    def get_context_data(self, **kwargs):
            context = super(Login_vw, self).get_context_data(**kwargs)

            idProspect_ses = self.request.session['idProspect']
            Prospect_qr = PROSPECT.objects.filter(id=idProspect_ses).values('id', 'email', 'pwd')

            context['email'] = Prospect_qr[0]['email']
            return context

    def form_valid(self, form):
            formData = form.clean()
            pwd_f = formData.get("password")

            if pwd_f == Prospect_qr[0]['pwd']:
                print "password correct"
            else:
                print "wrong password"

So, I tried do it with self like this, but I got an attribute error (object has no attribute Prospect_qr)

class Login_vw(FormView):
    form_class = Login_fr
    success_url = '/success/'
    template_name = 'login.html'

    def get_context_data(self, **kwargs):
            context = super(Login_vw, self).get_context_data(**kwargs)

            idProspect_ses = self.request.session['idProspect']
            self.Prospect_qr = PROSPECT.objects.filter(id=idProspect_ses).values('id', 'email', 'pwd')

            context['email'] = self.Prospect_qr[0]['email']
            return context

    def form_valid(self, form):
            formData = form.clean()
            pwd_f = formData.get("password")

            if pwd_f == self.Prospect_qr[0]['pwd']:
                print "password correct"
            else:
                print "wrong password"
QUHO
  • 445
  • 1
  • 4
  • 12

2 Answers2

0

Actually you can not do that. Why would you want to populate record_qr on view initialization and not where you'd want to use it ? For example if I want to use record_qr once the form has been submitted, I would do it in the form_valid method. If you would want to use it in multiple functions why not create a separate function in your view. For example:

class MyForm(FormView):
    record_qr = None

    def get_record_qr(self):
        if self.record_qr:
            return self.record_qr
        elif hasattr(self, 'request'):
            self.record_qr = MyModel.objects.filter(id=self.request.session.get(value))
            return self.record_qr
        return None

and then call this function wherever you want to use it like:

def form_valid(self, form):
    record_qr = self.get_record_qr()
Amyth
  • 32,527
  • 26
  • 93
  • 135
  • Actually I need to use it in two methods at this moment, but maybe I could need it in more of them, but my mainly point is to keep the "DRY" concept and performance with not checking the database from each method. The way you told me looks cleaner, but I think it is still checking the database every time I call the get_record_qr method. Maybe if I can save the query result in a variable and then just use that variable instead. Do you know what is wrong with the @DomTomCat answer? I'm going to edit my question to add more details, I really appreciate your help! – QUHO Jun 16 '16 at 15:04
  • in the `get_record_qr` method set the the value of `record_qr` as an attribute to self. Check the updated answer. – Amyth Jun 16 '16 at 19:01
  • I'm sorry, but your solution gives me this error: `NameError: global name 'get_record_qr' is not defined` Maybe I have to declare the method some where? – QUHO Jun 17 '16 at 00:44
  • Yes declare the method within the form class. – Amyth Jun 17 '16 at 06:41
-1

you're defining class object variables (static), in contrast to instance object variables. See this question and answers. See also the python docs

What you probably want to use instance variables, which you'd usually define on a class constructor

class Login_vw(FormView):
    def __init__(self, *args, **kwargs):
        FormView.__init__(self, *args, **kwargs)
        form_class = Login_fr
        success_url = '/success/'
        template_name = 'login.html'

        # the constructor of FormView will set this (hopefully)
        someVar_ses = self.request.session['someVar']
        record_qr = MyModel.objects.filter(id=someVar_ses)
Community
  • 1
  • 1
DomTomCat
  • 8,189
  • 1
  • 49
  • 64