How do you update an attribute in a user profile in Django with a command?
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"
accounts
__init__.py
models.py
admin.py (we'll ignore this for now, it works fine) <br>
management
__init__.py
commands
__init__.py
generate_user.py
in settings.py we have AUTH_PROFILE_MODULE = 'accounts.UserProfile'
in models.py we have
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])
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: http://www.turnkeylinux.org/blog/django-profile#comment-7262
Next, we need to try to make our simple command. So in gen_user.py
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
to run, from your project directory, type python manage.py gen_user
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 us开发者_高级运维er 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.
Ideas? Thanks!!!!
user.profile
retrieves the profile, but nowhere do you ever make an attempt to actually save it. Put the result into a variable, mutate it, and then save it.
profile = user.profile
profile.age = 34
profile.save()
精彩评论