python 2.7 - How to make a custom manager work with non-default database in django? -
i want make custom manager models stored in database 'db2' don't have model.objects.using('db2').all()
. code models , custom manager
class viewerprofilemanager(models.manager): def get_query_set(self): return super(viewerprofilemanager,self).using('db2').get_query_set() # results in recursion error class viewerprofile(models.model): name = models.charfield(max_length=32) count = models.integerfield(blank=true,null=true) objects = models.manager() profiles = viewerprofilemanager() ------------- >>> viewerprofile.profiles.all() # maximum recursion depth exceeded error
what's wrong doing here , how make work?
you can call method on parent (using super()) or appropriate handling of _db attribute on manager(a string containing name of database use). if want return custom queryset class get_query_set method :
class viewerprofilemanager(models.manager): def get_query_set(self): qs = customqueryset(self.model) if self._db not none: qs = qs.using(self._db) return qs or use this: class viewerprofilemanager(models.manager): using="db" def get_query_set(self,request): return super(viewerprofilemanager,self).queryset(request).using(self.using) class viewerprofile(models.model): name = models.charfield(max_length=32) count = models.integerfield(blank=true,null=true) objects = models.manager() profiles = viewerprofilemanager()
self._db going affected name of database
Comments
Post a Comment