Python 创建超级用户时创建日期行
models.pyPython 创建超级用户时创建日期行,python,django,django-models,django-forms,django-admin,Python,Django,Django Models,Django Forms,Django Admin,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_len
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'对象没有属性'location'
“创建超级用户时
下面给出了我需要的样本
作为信号处理程序尝试此功能:
def location_title(sender, instance, created, **kwargs):
# Don't fire up on updates.
if not created:
return
# Only handle new superusers.
if not instance.is_superuser or not instance.is_active:
return
# Create a `Location` entry for new superuser.
l = Location(user_id=instance.pk)
l.save()
post_save.connect(location_title, sender=User)
将选项添加到模型字段:
Django CharField有一个命名参数choices
,允许您向最终用户提供一个可能值的列表,并在表单中对其进行适当的验证。iterable的格式如下所示,
。向字段传递choices
参数后,可以使用instance.get\u display()
方法访问连接到其内部值的显示值
iterable的选项可能如下所示:
class Location(models.Model):
class Title:
CLASSROOM = 'classroom'
PLAYGROUND = 'playground'
STAFF_ROOM = 'staff_room'
TITLE_CHOICES = (
(Title.CLASSROOM, 'Classroom'),
(Title.PLAYGROUND, 'Playground'),
(Title.STAFF_ROOM, 'Staff Room'),
)
user = models.ForeignKey(User,null=True)
title = models.CharField('Incident Type', max_length=200,choices=TITLE_CHOICES,default=Title.CLASSROOM)
parent_location_id = models.CharField('Parent Location', max_length=100, null=True, blank=True)
is_active = models.BooleanField('Is Active', default=True)
最终解决方案如下:
class Location(models.Model):
class Title:
CLASSROOM = 'classroom'
PLAYGROUND = 'playground'
STAFF_ROOM = 'staff_room'
BASE_LOCATION = Title.CLASSROOM
TITLE_CHOICES = (
(Title.CLASSROOM, 'Classroom'),
(Title.PLAYGROUND, 'Playground'),
(Title.STAFF_ROOM, 'Staff Room'),
)
user = models.ForeignKey(User,null=True)
title = models.CharField('Incident Type', max_length=200,choices=TITLE_CHOICES,default=Title.CLASSROOM)
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):
# Don't fire up on updates.
if not created:
return
# Only handle new superusers.
if not instance.is_superuser or not instance.is_active:
return
# Create a `Location` entry for new superuser.
base = Location(user_id=instance.pk, title=Location.BASE_LOCATION)
base.save()
for value, _ in Location.TITLE_CHOICES:
if value == Location.BASE_LOCATION:
continue
l = Location(user_id=instance.pk, title=value, parent_location_id=base.pk)
l.save()
post_save.connect(location_title, sender=User)
创建用户或超级用户时,将创建模型实例,但它没有相应的
位置
行。因此,访问instance.location.is\u active
时会出现错误
您可以首先更新信号处理程序以创建位置
实例,然后设置适当的属性。详情如下:
def location_title(sender, instance, created, **kwargs):
#also check for created flag
if created and instance.is_superuser:
location = Location(user=instance)
location.is_active=True
location.save()
post_save.connect(location_title, sender=User)
如果您想选择标题
字段,可以在字段中定义该字段。您对标题字段的定义不正确,请将其更改为
title = models.CharField('Incident Type', max_length=200, choices=TITLE,
default='Classroom')
您错误地使用了detfault=TITLE
而不是choices=TITLE
他们是否有机会实现这一点。这是什么django版本?我使用的是django 1.3.7,那么您必须阅读以下内容:1。如果您不愿意升级到Django 1.5,其中包括自定义用户模型,那么您必须使用@mariodev提到的配置文件。2.您看到的“UserProfile未定义”错误可能是因为您尚未定义AUTH_PROFILE_模块设置。再次查看上面的链接。3.您应该考虑为您的用户创建自定义模型管理器,这将允许您在创建超级用户时修改位置。见文件,但这在Django 1.3中可能不可用。上面的代码正在位置表中创建一行。我正在尝试在标题字段中包含选项,以便我必须在数据库中创建默认数据。我需要示例图片中给出的输出。您能指导我如何操作吗。如果您希望标题字段中有选项
,将命名参数选项
与值标题
一起使用。要设置默认值,请使用TITLE
元组的一个元素,即TITLE[0][0]将classick
设置为default
命名参数的默认值。选项元组是成对的元组,
其中internal\u value
应该是内部使用的值-在脚本、数据库等中。而display\u value
是显示给前端用户的值。刚才我检查了您的评论,看起来是另一种方法。你能解释一下怎么做吗?我试过信号处理程序,很抱歉,它没有在db中创建默认值和选项值。你所说的默认值和选项值在db中是什么意思?当前,信号处理程序使用默认值创建单个位置
条目。是否要为每个超级用户创建所有4个Location
对象?如果是创建的而不是实例。是否\u superuser:-->在数据库中创建了一行数据。我想将上述选项包括在标题字段中。如何将其包括在标题字段中。@user2086641,您还可以设置位置。title='classification'
。请注意,Location
与User
具有OneToOne
关系,因此一个用户不能有多个位置对象(具有不同标题)。是否可以使用用户表的ForeignKey关系。@user2086641,检查使用choices
属性的更新答案。@user2086641,是,使用ForeignKey()。