Sebastian Olsen
Sebastian Olsen

Reputation: 10888

How to extend the user model in django 1.9?

I've been searching around trying to figure this out, I want to add simple things to my Profile model like avatar, contact info, etc. However when following tutorials that I found online on how to do this, I am only met with errors.

This is what my model looks like (tracks/models.py):

from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.images import get_image_dimensions
from django.contrib.auth.models import User

...

class Profile(models.Model):

    def generate_user_folder_avatar(instance, filename):
        return "uploads/users/%s/%s.png" % (instance.user, 'avatar')

    user = models.OneToOneField(User)

    avatar = models.ImageField(upload_to=generate_user_folder_avatar,validators=[is_square_png])

    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)

    post_save.connect(create_user_profile, sender=User)

I've set the AUTH_PROFILE_MODULE = 'tracks.Profile' in settings.py but when I run my server I get this error:

NameError: name 'post_save' is not defined

Any idea what I am doing wrong here? I'm using Django 1.9 and Python 3

Upvotes: 1

Views: 3390

Answers (1)

Bruno Gelb
Bruno Gelb

Reputation: 5662

NameError: name 'post_save' is not defined

you should do the import:

from django.db.models.signals import post_save

note #1: you may be interested in the fact that Django provides more explicit and clear way to extend User model: https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-django-s-default-user

note #2: you probably want to connect signals not somewhere in models, but like that: https://stackoverflow.com/a/21612050/699864

Upvotes: 6

Related Questions