My url has a keyword "shop_name" variable. There's also the Shop model with "name" field.
In my ListView class I need to make repeating queries to Shop model to get a unicode variable from Shop.get_type() method. Depending on the result, a proper template directory is selected or queryset (Using subclassed django models).
Here's the code.
class OfferList(ListView):
def get_template_names(self):
shop = Shop.objects.get(name=self.kwargs['shop_name'])
return ["shop/%s/offer_list" % shop.get_type()]
def get_queryset(self):
shop = Shop.objects.get(name=self.kwargs['shop_name'])
Offer = shop.get_offers_model()
return Offer.objects.all()
def get_context_data(self, **kwargs):
# again getting shop instance here ...
shop = Shop.objects.get(name=self.kwargs['shop_name'])
context = super(OfferList, self).get_context_data(**kwargs)
context['shop'] = shop
return context
Question is what is the best way, so I can get some var (shop in this case) available for all methods ? I'm not a python guru (may be the basic problem). I've tried with init overriding but then I couldn't get exchange_name (specified in urls.py) to get the right "shop" instance. I would like to avoid repeating.
Thanks