문제가 있습니다.Python, Django. 사전 특성이있는 사용자 모델 확장
# myapp views.py
from django.views.generic.edit import FormView
from django.contrib.auth.forms import AuthenticationForm
class LoginView(FormView):
form_class = AuthenticationForm
template_name = 'registration/login.html'
def form_valid(self, form):
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
# default value for games is None
user.userprofile.games = {}
# now it should be an empty dict
login(self.request, user)
return redirect('/game')
class Index(FormView):
def dispatch(self, request, *args, **kwargs):
profile = request.user.get_profile()
print profile.games # Prints 'None'
글쎄, 내 질문은 다음과 같습니다 : 왜 '인쇄 프로파일
# myapp models.py
from django.contrib.auth.models import User
from django.db import models
class UserProfile(models.Model):
user = models.OneToOneField(User)
games = None
def create_user_profile(sender, instance, created, **kwargs):
if not created:
profile, created = UserProfile.objects.get_or_create(user=instance)
models.signals.post_save.connect(create_user_profile, sender=User)
지금 내가 loggin에의 할 때'게임 'ATTR을 변경하려면 : 이 메신저 기본 사용자 모델을 확장하려고하는 방법이다 .games 'prints'None '그리고 로그인 할 때 게임 attr을 어떻게 바꿀 수 있습니까?
'저장'을 잊어 버리셨습니까? 내 추측이야. 게다가 디폴트로'games'는 None이고'{}'은 빈 dict이므로 None입니다. – user1012451
@ user1012451 '저장'이 도움이되지 않은 것 같습니다. Btw,'d = {}; print d'는'None' 대신'{}'를 표시합니다 – malinoff