Two profile types with OneToOneField relationship with User

21 views
Skip to first unread message

elto...@gmail.com

unread,
Nov 26, 2016, 9:23:54 AM11/26/16
to Django users
Hi guys

I'm on Django 1.10, and as described shortly here: https://dpaste.de/Bgao, I have created an abstract user profile which is tied to Django's User model with OneToOneField relationship. So I have AbstractProfile, and then there are IndividualProfile and BusinessProfile, each of which inherits from AbstractProfile.

The problem I have, is that, whenever I have a user object instance (i.e. u = User.objects.get(pk=1234)), I'm not sure how to determine whether the profile is of type Individual or Business.

So either u.individualprofile or u.businessprofile throws an error, and obviously I want to avoid having errors.

Any help is appreciated. Thanks

ludovic coues

unread,
Nov 26, 2016, 9:36:42 AM11/26/16
to django...@googlegroups.com
Have you tried `getattr(u, 'individualprofile', None)` ?
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/3dac6122-c209-415e-af62-387988c384d3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

elto...@gmail.com

unread,
Nov 26, 2016, 9:41:01 AM11/26/16
to Django users
It seems to work. Thanks
Reply all
Reply to author
Forward
0 new messages