I have for two similar models with one common Manager, that returns queryset for current logged user. I want User_Places.current_user.get(place=XXX) returns existing User_Place() object or empty User_Places(), when DoesNotExist exception raised. How is it possible? CurrentUserManager doesn't know anything about User_Places and User_Projects models, if I'm right?
I just don't want to write always:
try:
places = User_Places.current_user.get(place=XXX)
except:
places = User_Places()
Code of my models:
class User_Places(models.Model):
place = models.ForeignKey('Place')
user = models.ForeignKey(User)
current_user = CurrentUserManager()
class User_Projects(models.Model):
project = models.ForeignKey('Project')
user = models.ForeignKey(User)
current_user = CurrentUserManager()
class CurrentUserManager(models.Manager):
def get_query_set(self):
user = threadlocals.get_current_user()
qs = super(CurrentUserManager, self).get_query_set()
if user:
qs = qs.filter(user=user)
return qs
def get(self, *args, **kwargs):
try:
object = super(CurrentUserManager, self).get(*args, **kwargs)
except:
object = ????????????????????????
return object