create row of date while creating superuser

后端 未结 2 1777
再見小時候
再見小時候 2020-12-21 15:18

models.py

TITLE = (
    (\'Classroom\', \'Classroom\'),
    (\'Playground\', \'Playground\'),
    (\'Staff Room\',\'Staff Room\'),
)

class Location(models.M         


        
2条回答
  •  时光说笑
    2020-12-21 16:18

    When you create the user or superuser the model instance is created but it does not have corresponding location row. Hence, accessing instance.location.is_active gives you the error.

    You can update the signal handler to create the location instance first and then set appropriate attributes. As below:

    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)
    

    If you want to choices for title field you can define that in field. Your definition of the title field is not correct, change it to

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

    You are incorrectly using detfault=TITLE instead of choices=TITLE.

提交回复
热议问题