title: flask how to preserve instance of SQLAlchemyobject
body: <p>I want to load a global menu for any template. The structure of the Query is always the same...</p>
  <pre><code>class ProductCats(db.Model):
     def __init__(self,
                lang=None,
                catname=None,
                catid=None,
                update=False,
                active=False,
                create=False,
                ):
  
                self.categories = db.session.query(ProductCats, ProductCatsTranslations)
                if lang:
                    self.categories = self.categories.filter(ProductCatsTranslations.language_code==lang)
     
                if catid:
                    self.categories = self.categories.filter(ProductCatsTranslations.catid==catid)
     
                if active:
                    self.categories = self.categories.filter(ProductCats.active==active)
                                                                                                                                                                                             
     
                self.categories = self.categories.filter(ProductCatsTranslations.catid==ProductCats.catid).\
                order_by(ProductCatsTranslations.sort)
     
  
                self.dicted_by_catid = {}
     
                for c, ct in self.categories:
                    c.ct = ct
                    self.dicted_by_catid[c.catid] = c 
  
       # then I have a method within this class
       def by_catid(self, catid=None):
            return self.dicted_by_catid[catid]
  </code></pre>
  <p>in <strong>init</strong>.py of my flask app</p>
  <pre><code>@app.before_request
  def before_request():
      from models import ProductCats
      g.productcats = ProductCats(lang=lang)
  </code></pre>
  <p>So I can easily access my productcats everywhere on flaskapp. But it initiates one Query everytime instead of getting it from the dictonary &quot;by_catid&quot;. I call something like this. e.g. in the template or on views or where ever in this app.</p>
  <pre><code>g.productcats.by_catid(catid=12).name
  g.productcats.by_catid(catid=172).name
  </code></pre>
  <p>This produces two Queries instead of one from <strong>init</strong> of ProductCats</p>
  <p>When I manually set up the same db.query of <strong>init</strong> of ProductCats() somewhere else in my application e.g. on my homeview I can call the attribute any time without it creates new Query everytime.</p>
  <p>There seems to be something special,  that g.object is calling the init method of ProductCats everytime again? But when I add a simple debugoutput in my ProductCats init it appears only ONE time. Also the relationship to ProductCatsTranslation is lazy-joined. Also it works in my homeview when not using flask-global (g).</p>
  <p>How would I have my productcats all over the app without generating one Query for every category while I can still use the instance of my Productcats in my templates e.g.</p>
  <pre><code>g.productcats.by_catid(catid=12).name
  g.productcats.by_catid(catid=14).otherattribute
  g.productcats.by_catid(catid=12).active
  </code></pre>