basic Django 10,000 foot view of users (associating Django User with model class)

59 views
Skip to first unread message

Mitchell McConnell

unread,
Dec 25, 2015, 8:56:57 AM12/25/15
to Django users
I am just getting started in Django, and probably need to slow down, but I am trying to work through
the documentation *and* work on a real app that I have created in the past as a Python desktop app
using WxPython.

The app manages widgets for employees, who belong to an Organization (part of my model).  My assumption is that
the Django web site will allow Django Users to log in, who must belong to an Organization.

Then, that User will only see the widgets and employees that belong to their own Organization.  Note that
the Employees being managed (again, part of my model) are *NOT* going to need to be Django users on
the website... only the Organizations widget administrator(s).

I assume that I need to 
1. Extend the Django User class to include which Organization they belong to.


But the example I have found so far shows adding a foreign key to the model table, e.g.


Clearly, my Organization entity can have multiple Django users, and every Django user of my app
must belong to one and only one of my Organizations.

When I added the code from the link above to try and do #1 above, but without adding the example's
one-to-one User field to the Organization, I get the error:

ERRORS:
<class 'certapp.admin.OrganizationInline'>: (admin.E202) 'certapp.Organization' has no ForeignKey to 'auth.User'.


This seems like a pretty common way to organize a traditional app... what am I missing, or can
someone point me to an example that does what I want?

Thanks,

Mitch

Mike Dewhirst

unread,
Dec 25, 2015, 6:20:07 PM12/25/15
to django...@googlegroups.com
On 26/12/2015 12:53 AM, Mitchell McConnell wrote:
> I am just getting started in Django, and probably need to slow down, but
> I am trying to work through
> the documentation *and* work on a real app that I have created in the
> past as a Python desktop app
> using WxPython.
>
> The app manages widgets for employees, who belong to an Organization
> (part of my model). Â My assumption is that
> the Django web site will allow Django Users to log in, who must belong
> to an Organization.
>
> Then, that User will only see the widgets and employees that belong to
> their own Organization. Â Note that
> the Employees being managed (again, part of my model) are *NOT* going to
> need to be Django users on
> the website... only the Organizations widget administrator(s).
>
> I assume that I need toÂ
> 1. Extend the Django User class to include which Organization they
> belong to.
>
>
> But the example I have found so far shows adding a foreign key to the
> model table, e.g.
>
> https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
>
> Clearly, my Organization entity can have multiple Django users, and
> every Django user of my app
> must belong to one and only one of my Organizations.
>
> When I added the code from the link above to try and do #1 above, but
> without adding the example's
> one-to-one User field to the Organization, I get the error:

You probably don't want a one-to-one but rather a one-to-many. One org
to many users. So you don't really want a ForeignKey to auth.user

In your extended user model you just need a foreign key to Organization.
That user will then only be able to connect to a single organization.
Lots of other users will however be able to connect to the same
organization.

Mike

>
> ERRORS:
> <class 'certapp.admin.OrganizationInline'>: (admin.E202)
> 'certapp.Organization' has no ForeignKey to 'auth.User'.
>
>
>
> This seems like a pretty common way to organize a traditional app...
> what am I missing, or can
> someone point me to an example that does what I want?
>
> Thanks,
>
> Mitch
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/f0616bb2-b36e-4097-bfa2-09081e073d13%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/f0616bb2-b36e-4097-bfa2-09081e073d13%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Mitchell McConnell

unread,
Dec 26, 2015, 7:04:17 AM12/26/15
to Django users
Mike, thanks for the reply.  I will try and implement this...
Reply all
Reply to author
Forward
0 new messages