Question on Extending the existing User model documentation

54 views
Skip to first unread message

Larry Martell

unread,
Apr 10, 2015, 9:07:07 AM4/10/15
to django...@googlegroups.com
Reading this: https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#extending-the-existing-user-model

I see this:

These profile models are not special in any way - they are just Django
models that happen to have a one-to-one link with a User model. As
such, they do not get auto created when a user is created, but a
django.db.models.signals.post_save could be used to create or update
related models as appropriate.

I have a related model set up just like this, and when I new user is
created a row in the related table is created. So what does this
comment in the docs mean when it says "they do not get auto created
when a user is created"?

Mike Dewhirst

unread,
Apr 11, 2015, 9:28:29 AM4/11/15
to django...@googlegroups.com
I suspect you have established the profile model in the Admin where it
appears in-line with the user model. It is the Admin adding the 1:1
foreign key to the profile which creates the record on saving.

Remember, the Admin is an app and not part of core Django. The docs you
quote are about core Django.

If you are not using the Admin you might need to use the post_save
signal for the user model to create the profile model.

Mike

>

Larry Martell

unread,
Apr 11, 2015, 9:35:50 AM4/11/15
to django...@googlegroups.com
That makes sense, but just above the text I quoted from the docs, the
code example shown uses the Admin, so it's confusing, at least to me.

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from my_user_profile_app.models import Employee

# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'

# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (EmployeeInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)from

django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from my_user_profile_app.models import Employee

# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'

# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (EmployeeInline, )

# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Reply all
Reply to author
Forward
0 new messages