【发布时间】:2012-12-13 03:10:30
【问题描述】:
我假设这是因为我的超级用户依赖于尚无现有数据的 UserProfile。 我的模型看起来像
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User) # required
location = models.CharField(max_length=100)
age = models.PositiveIntegerField(blank=True,null=True)
contribution_points = models.PositiveIntegerField()
#acheivements = models.ManyToMany()
def create_user_profile(sender,instance,created,**kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
但是,我最终得到以下错误:
django.db.utils.DatabaseError: (1146, "Table 'savory_db.login_userprofile' doesn't exist")
尽管刚刚跑了syncdb
我的模型是否有任何会导致此错误的矛盾字段。 UserProfile 不应该应用于超级用户吗?我应该如何防止这种情况?
【问题讨论】:
-
您是否已将 UserProfile 应用添加到您的设置中?如果不是,则不会使用 syncdb 创建表。您的代码看起来不错(至少乍一看——我已经实现了一个非常相似的保存后方法,没有任何问题)。
-
另外,为了清楚起见,您的陈述“我假设这是因为我的超级用户依赖于 UserProfile”是不正确的——这里的依赖关系正好相反。事实上,如果您的 createsuperuser 命令完全失败,我会感到惊讶——我敢打赌它创建了超级用户,只是未能创建关联的配置文件。
-
另外,您是否知道在 Django 1.5 中,开发人员允许自定义 Django User 模型?如果您改为扩展 User 模型,则不必创建单独的 UserProfile(不知道您是否真的打算将它们分开,只是一个想法)
-
感谢您纠正我的错误陈述。我目前正在使用 1.4,但我知道添加的自定义。 :)
-
那么是否您将 UserProfile 添加到您的设置中?
标签: django