I have two models like this:
class Type1Profile(models.Model):
user = models.OneToOneField(User, unique=True)
...
class Type2Profile(models.Model):
How about using try/except blocks?
def get_profile_or_none(user, profile_cls):
try:
profile = getattr(user, profile_cls.__name__.lower())
except profile_cls.DoesNotExist:
profile = None
return profile
Then, use like this!
u = request.user
if get_profile_or_none(u, Type1Profile) is not None:
# do something
elif get_profile_or_none(u, Type2Profile) is not None:
# do something else
else:
# d'oh!
I suppose you could use this as a generic function to get any reverse OneToOne instance, given an originating class (here: your profile classes) and a related instance (here: request.user).