python - create row of date while creating superuser -
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 , not instance.location.is_active: instance.location.is_active=true instance.location.save() post_save.connect(location_title, sender=user)
i want insert default data database conditions.this should happen while creating superuser via manage.py createsuperuser
comment.
i don't know possible django,but requirement.i tried above code.i getting error "attributeerror: 'user' object has no attribute 'location' " while creating superuser.
the sample required given below
try function signal handler:
def location_title(sender, instance, created, **kwargs): # don't fire on updates. if not created: return # handle new superusers. if not instance.is_superuser or not instance.is_active: return # create `location` entry new superuser. l = location(user_id=instance.pk) l.save() post_save.connect(location_title, sender=user)
adding choices model field:
django charfield has named argument choices
allows give end user list of possible values, , proper validation of them in forms. format of iterable follows <internal_value>, <display_value>
. once field has been passed choices
argument, can access display value connected it's internal value instance.get_<field_name>_display()
method.
the choices iterable this:
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)
the final solution following:
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 on updates. if not created: return # handle new superusers. if not instance.is_superuser or not instance.is_active: return # create `location` entry new superuser. base = location(user_id=instance.pk, title=location.base_location) base.save() 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)
Comments
Post a Comment