Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Django use the same variable in different functions

What I am trying to do is, fetching data from a third party app and show it on the page. I will get the data in get_context_data function and use the same data in get_initial method as well. I could not manage to do that. Is there any way to do that?

Example Code


class UpdateView(generic.FormView):
    template_name = 'test.html'
    form_class = myForm
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        MYVARIABLE = fetch_data_from_3rd_party_app()
        context["MYVARIABLE"] = MYVARIABLE
        return context

    def get_initial(self):
        initial = super().get_initial()

        # I want to assign MYVARIABLE.data to initial["data"] here.
        initial["data"] = MYVARIABLE

        return initial

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

There's a couple of options

First there's the approach that the generic views from Django take and store the variable on self, assign it at some early point in the request (dispatch, get, post, etc) so that it's available to whoever needs it

class UpdateView(generic.FormView):

    def dispatch(self, request, *args, **kwargs):
        self.myvariable = fetch_data_from_3rd_party_app()
        return super().dispatch(request, *args, **kwargs)
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["MYVARIABLE"] = self.myvariable
        return context

    def get_initial(self):
        initial = super().get_initial()
        initial["data"] = self.myvariable
        return initial

I'm quite partial to using a cached property, assigning to self outside of __init__ feels slightly wrong to me (although there is nothing really wrong with it)

from django.utils.functional import cached_property

class UpdateView(generic.FormView):

    @cached_property
    def myvariable(self):
        return fetch_data_from_3rd_party_app()
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["MYVARIABLE"] = self.myvariable
        return context

    def get_initial(self):
        initial = super().get_initial()
        initial["data"] = self.myvariable
        return initial
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!