내 UserProfile의 필드에 데이터를 저장하려고하면 Django에서 오류가 발생합니다.django에서 userProfile의 필드를 업데이트하는 방법
나는 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'
, 난이 형식 오류를 얻을 :
(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
테스트 된 기능 :
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()