【发布时间】:2013-07-22 07:34:51
【问题描述】:
models.py
TITLE = (
('Classroom', 'Classroom'),
('Playground', 'Playground'),
('Staff Room','Staff Room'),
)
class Location(models.Model):
user = models.ForeignKey(User,null=True)
title = models.CharField('Incident Type', max_length=200,default=TITLE)
parent_location_id = models.CharField('Parent Location', max_length=100, null=True, blank=True)
is_active = models.BooleanField('Is Active', default=True)
def location_title(sender, instance, created, **kwargs):
if instance.is_superuser and not instance.location.is_active:
instance.location.is_active=True
instance.location.save()
post_save.connect(location_title, sender=User)
我想在特定条件下将默认数据插入数据库。这应该在通过manage.py createsuperuser 注释创建超级用户时发生。
我不知道使用 django 是否可行,但这是必需的。我尝试使用上面的代码。我收到错误“AttributeError: 'User' object has no attribute 'location' " 同时创建超级用户。
我需要的样本如下
【问题讨论】:
-
他们是否有机会实现这一点。
-
这是什么django版本?
-
我使用的是 Django 1.3.7
-
1.如果您不愿意升级到包含自定义用户模型的 Django 1.5,那么您将不得不使用配置文件,如 @mariodev 所述。 2. 您看到的错误“未定义用户配置文件”可能是因为您尚未定义 AUTH_PROFILE_MODULE 设置。再次查看上面的链接。 3. 您应该考虑为您的用户创建一个自定义模型管理器,这将允许您在创建超级用户时修改位置。请参阅文档here,但这在 Django 1.3 中可能不可用
标签: python django django-models django-forms django-admin