如何在 django 中更新 userProfile 中的字段

发布于 2024-12-28 20:24:16 字数 2239 浏览 0 评论 0原文

当我尝试将数据保存到我的 UserProfile 中的字段时,Django 会引发类型错误。

我认为我正确扩展了 UserProfile。

我的 userProfile 模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True, related_name='profile')
    ....
    initials = models.CharField(max_length=5)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

在我的设置中添加了:

AUTH_PROFILE_MODULE = 'timepiece.UserProfile'

当我运行应将值插入到从 User 继承的字段中的测试时,我收到 TypeError:

(erp)BAir:website jorrit$ ./manage.py test timepiece.PivotalTest.test_update_users
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_update_users (timepiece.tests.pivotal.PivotalTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jorrit/virtualenvs/erp/erp/apps/timepiece/tests/pivotal.py", line 73, in test_update_users
    UserProfile.sync_pivotal_users(self.tracker, 450001)
  File "/Users/jorrit/virtualenvs/erp/erp/apps/timepiece/models.py", line 44, in sync_pivotal_users
    initials = m.initials,)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 365, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
TypeError: 'username' is an invalid keyword argument for this function

----------------------------------------------------------------------
Ran 1 test in 1.857s

The function that is test:

def sync_pivotal_users(tracker, project_id):
    members = tracker.get_memberships_project(project_id)

    for m in members:
    #check if member exist in db
        try:
            #to know if m.id exists in current db
            UserProfile.objects.get(pivotal_member_id=m.id)
            #if not then create a new user
        except UserProfile.DoesNotExist:
            u = UserProfile(pivotal_member_id = m.id,
                            username = m.name, #code breaks here
                            email = m.email, # and here 
                            initials = m.initials,)
            u.save()

When i try to save data to fields in my UserProfile Django raises an typeerror.

I thought that i extended the UserProfile correctly.

My userProfile model:

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True, related_name='profile')
    ....
    initials = models.CharField(max_length=5)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

In my settings i have added:

AUTH_PROFILE_MODULE = 'timepiece.UserProfile'

When i run a test which should insert values into inherited fields from User, i get an TypeError:

(erp)BAir:website jorrit$ ./manage.py test timepiece.PivotalTest.test_update_users
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_update_users (timepiece.tests.pivotal.PivotalTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/jorrit/virtualenvs/erp/erp/apps/timepiece/tests/pivotal.py", line 73, in test_update_users
    UserProfile.sync_pivotal_users(self.tracker, 450001)
  File "/Users/jorrit/virtualenvs/erp/erp/apps/timepiece/models.py", line 44, in sync_pivotal_users
    initials = m.initials,)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 365, in __init__
    raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
TypeError: 'username' is an invalid keyword argument for this function

----------------------------------------------------------------------
Ran 1 test in 1.857s

The function which is tested:

def sync_pivotal_users(tracker, project_id):
    members = tracker.get_memberships_project(project_id)

    for m in members:
    #check if member exist in db
        try:
            #to know if m.id exists in current db
            UserProfile.objects.get(pivotal_member_id=m.id)
            #if not then create a new user
        except UserProfile.DoesNotExist:
            u = UserProfile(pivotal_member_id = m.id,
                            username = m.name, #code breaks here
                            email = m.email, # and here 
                            initials = m.initials,)
            u.save()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄凉少年不暖心 2025-01-04 20:24:16

文档中概述的方法背后的想法< /a> 是当创建一个新的 User 对象时,您附加一个处理程序,该处理程序又创建一个 UserProfile 对象:

如果配置文件不存在,则 get_profile() 方法不会创建配置文件。您需要为用户模型的 django.db.models.signals.post_save 信号注册一个处理程序,并在处理程序中,如果创建为 True,则创建关联的用户配置文件

因此在通过成员的循环中,您应该创建 User 对象,而不是 UserProfile 对象,因为它们将通过信号自动创建。

目前尚不清楚如何获取密码(注册过程),但是一旦获得密码,您可以使用以下命令更新该用户的个人资料

profile = current_user_obj.get_profile()
profile.initial = "some value"
profile.save() 

The idea behind the approach outlined in the documentation is that when a new User object is created, you attach a handler that in turn creates a UserProfile object:

The method get_profile() does not create a profile if one does not exist. You need to register a handler for the User model's django.db.models.signals.post_save signal and, in the handler, if created is True, create the associated user profile

so in your loop through members, you should be creating User objects, not UserProfile objects, as they will automatically be created via the signal.

It's not really clear how you are getting passwords (the sign up process), but once you have those, you can update the profile for that user using

profile = current_user_obj.get_profile()
profile.initial = "some value"
profile.save() 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文