我一直在寻找解决这个问题,我想在我的Profile
模型中添加简单的内容,例如头像,联系信息等。但是,当我按照我在网上找到的关于如何找到的教程时这样做,我只遇到了错误。
这就是我的模型(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)
我在settings.py中设置了AUTH_PROFILE_MODULE = 'tracks.Profile'
但是当我运行服务器时出现此错误:
NameError: name 'post_save' is not defined
知道我在这里做错了吗?我使用的是Django 1.9和Python 3
答案 0 :(得分:6)
NameError: name 'post_save' is not defined
你应该进行导入:
from django.db.models.signals import post_save
note#1 :您可能会对Django提供更明确,更清晰的扩展用户模型的方式感兴趣: https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-django-s-default-user
note#2 :您可能希望连接不在模型中某处的信号,但是这样:https://stackoverflow.com/a/21612050/699864