title: How do you update an attribute in a user profile in Django with a command?
body: <p>In Django, the standard way to add additional information to be associated with a user is to use a user profile.  To do this, I have an app called, "accounts"</p>
  
  <pre><code>accounts
     __init__.py 
     models.py 
         admin.py  (we'll ignore this for now, it works fine) &lt;br&gt;
         management 
              __init__.py 
              commands 
                   __init__.py 
                   generate_user.py 
  </code></pre>
  
  <p>in settings.py we have AUTH_PROFILE_MODULE = 'accounts.UserProfile'</p>
  
  <p>in models.py we have</p>
  
  <pre><code>from django.db import models 
  from django.contrib.auth.models import User
  # Create your models here.    
  class UserProfile(models.Model):
      user = models.ForeignKey(User, unique=True)
      age=models.IntegerField()
      extra_info=models.CharField(max_length=100,blank=True)
  User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])    
  </code></pre>
  
  <p>The last line makes use of python decorators to either get a user profile object if it already exists, or to return an existing one.   This code is taken from:
  <a href="http://www.turnkeylinux.org/blog/django-profile#comment-7262" rel="nofollow">http://www.turnkeylinux.org/blog/django-profile#comment-7262</a></p>
  
  <p>Next, we need to try to make our simple command.  So in gen_user.py</p>
  
  <pre><code>from django.core.manaement.base import NoArgsCommand
  from django.db import models
  from django.contrib.auth.models import User
  from accounts.models import UserProfile
  import django.db.utils
  
  
  class Command(NoArgsCommand):
  help='generate test user'
  def handle_noargs(self, **options):
      first_name='bob'; last_name='smith'
      username='bob' ; email='bob@bob.com'
      password='apple'
      #create or find a user
      try:
          user=User.objects.create_user(username=username,email=email,password=password)
      except django.db.utils.IntegrityError:
          print 'user exists'
          user=User.objects.get(username=username)
      user.firstname=first_name
      user.lastname=last_name
      user.save() #make sure we have the user before we fiddle around with his name
      #up to here, things work.
      user.profile.age=34
      user.save()
      #test_user=User.objects.get(username=username)
      #print 'test', test_user.profile.age
      #test_user.profile.age=23
      #test_user.save()
      #test_user2=User.objects.get(username=username)
      #print 'test2', test_user2.profile.age
  </code></pre>
  
  <p>to run, from your project directory, type python manage.py gen_user</p>
  
  <p>The question is, why doesn't the age update?  I suspect that this is a case of me catching
  an instance instead of the real object, bet
  everything that I've tried from using user.userprofile_set.create to using setattr, etc. has failed and I'm running out of ideas.  Is there a better pattern?  Ideally, I would like to just be able feed in a dict to update the userprofile, but for now, I can't see how to even update a single parameter.  Also, even when I have been able to create a user with one parameter (the age, which is required), I have not been able to later update the additional parameter.  I can't remove or delete the old userprofile and blast in a new one because of the foreignkey relation.  </p>
  
  <p>Ideas?  Thanks!!!!</p>