Tuesday, July 23, 2013

adding data in db related to user model

models.py

TITLE = (      ('Classroom', 'Classroom'),      ('Playground', 'Playground'),      ('Staff Room','Staff Room'),  )    class Location(models.Model):      user = models.OneToOneField(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)

I want to insert the default data into database with certain conditions.This should happen while creating superuser via manage.py createsuperuser comment.

I don't know it is possible with django,but it is the requirement.I tried with the above code.I am getting the error "AttributeError: 'User' object has no attribute 'location' " while creating superuser.

Getting this error while creating superuser.

Traceback:

Traceback (most recent call last):    File "manage.py", line 14, in <module>      execute_manager(settings)    File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager      utility.execute()    File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute      self.fetch_command(subcommand).run_from_argv(self.argv)    File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv      self.execute(*args, **options.__dict__)    File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute      output = self.handle(*args, **options)    File "/usr/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 134, in handle      User.objects.create_superuser(username, email, password)    File "/usr/lib/python2.7/site-packages/django/contrib/auth/models.py", line 144, in create_superuser      u.save(using=self._db)    File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 460, in save      self.save_base(using=using, force_insert=force_insert, force_update=force_update)    File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 570, in save_base      created=(not record_exists), raw=raw, using=using)    File "/usr/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send      response = receiver(signal=self, sender=sender, **named)    File "/root/Projects/ir_uploaded_copy/ir/setting/models.py", line 32, in location_title      if instance.is_superuser  and not instance.location.is_active and not instance.location.user.id:    File "/usr/lib/python2.7/site-packages/django/db/models/fields/related.py", line 239, in __get__      rel_obj = self.related.model._base_manager.using(db).get(**params)    File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 349, in get      % self.model._meta.object_name)  setting.models.DoesNotExist: Location matching query does not exist

The sample what i required is attached as image,please help me in doing this.





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate