Python 如何使用命令更新Django中用户配置文件中的属性?

Python 如何使用命令更新Django中用户配置文件中的属性?,python,django-models,Python,Django Models,在Django中,添加与用户关联的附加信息的标准方法是使用用户配置文件。为此,我有一个名为“帐户”的应用程序 最后一行使用python decorators获取用户配置文件对象(如果它已经存在),或者返回一个现有的用户配置文件对象。此代码取自: 接下来,我们需要尝试使用简单的命令。所以在gen_user.py中 from django.core.manaement.base import NoArgsCommand from django.db import models from djang

在Django中,添加与用户关联的附加信息的标准方法是使用用户配置文件。为此,我有一个名为“帐户”的应用程序

最后一行使用python decorators获取用户配置文件对象(如果它已经存在),或者返回一个现有的用户配置文件对象。此代码取自:

接下来,我们需要尝试使用简单的命令。所以在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
要运行,请在项目目录中键入python manage.py gen_user

问题是,为什么年龄没有更新?我怀疑这是我被抓的案子 一个实例而不是真实对象,打赌 从使用user.userprofile_set.create到使用setattr等,我尝试过的一切都失败了,我的想法也没有了。有更好的模式吗?理想情况下,我只想输入一个dict来更新userprofile,但现在,我甚至不知道如何更新一个参数。此外,即使我已经能够使用一个参数(年龄,这是必需的)创建一个用户,我以后也无法更新附加参数。由于foreignkey关系,我无法删除或删除旧的userprofile并在新的userprofile中爆炸


想法?谢谢

user.profile
检索配置文件,但您从未尝试实际保存它。将结果放入变量中,对其进行变异,然后保存

profile = user.profile
profile.age = 34
profile.save()

很抱歉这么慢,但我有以下内容:user.profile.age=34 user.save(),感谢您的快速响应!很抱歉这么慢,但是把它放到变量中有什么帮助呢?不应该:user.profile.age=34;user.save();哪个更新附加到用户工作的配置文件?user.profile.save()呢?
user
中没有显示“保存配置文件”。多次访问
user.profile
也没用,因为它每次都会返回一个新的模型。我刚刚尝试了这个,但是由于年龄没有定义,它会死掉的……再次感谢!我想我之前遇到了数据损坏。这是有效的,我必须更加努力去理解它,但是谢谢!!!
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
profile = user.profile
profile.age = 34
profile.save()