创建超级用户时创建日期行

时间:2013-07-23 09:42:52

标签: 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_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' “在创建超级用户的同时。

我需要的样本如下所示

Sample output

2 个答案:

答案 0 :(得分:4)

尝试此函数作为信号处理程序:

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,它允许您为最终用户提供可能值的列表,并在表单中对它们进行适当的验证。可迭代的格式如下<internal_value>, <display_value>。字段传递choices参数后,您可以使用instance.get_<field_name>_display()方法访问与其内部值相关联的显示值。

可迭代的选择可能如下所示:

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)

答案 1 :(得分:3)

创建用户或超级用户时,会创建模型实例,但它没有相应的location行。因此,访问instance.location.is_active会给您带来错误。

您可以更新信号处理程序以首先创建location实例,然后设置适当的属性。如下:

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字段,可以在字段中定义该字段。您对title字段的定义不正确,请将其更改为

title = models.CharField('Incident Type', max_length=200, choices=TITLE,  
                         default='Classroom') 

您错误地使用detfault=TITLE而不是choices=TITLE