我真的很想用Django構建這個應用程序,讓人們註冊並創建可以編輯的用戶實例。每個User實例已經與OneToOne的UserProfile鏈接,因爲我不想混淆原始User模型。如果該用戶登錄,UserProfile將具有他/她可以註冊遊戲的字段。如何鏈接這兩個模型,以便更新同一個實例?
即,比利想報名參加星期一粉碎近戰。他登錄後,單擊表單上的選項,鏈接到用戶的UserProfile將更新註冊的遊戲選項和用戶標籤到用戶配置文件。
鏈接到用戶的用戶配置文件的部分工作正常,但我不知道如何使用新的錦標賽註冊表更新UserProfile,以便它可以更改鏈接到記錄用戶的UserProfile字段在
Django模型:
class UserProfile(models.Model):
#User profile for registered users. SEPARATE USERBASE TO PLAYER_RANKING
#To Do: add more customizeability and more access for registered.
#weekly e-mails, ability to register for weeklies...
user = models.OneToOneField(User)
picture = models.ImageField(upload_to='profile_images', blank=True)
MON = 'ME'
TUE = 'S4'
THR = 'PM'
reg_game_choices = (
(MON, "Melee"),
(TUE, "Smash 4"),
(THR, "PM"),
)
reg_game_choice = models.CharField(max_length=2,
choices=reg_game_choices,
default="")
user_tag = models.CharField(max_length=60, default = "")
def __str__(self):
return self.user.username
形式:
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'password')
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('picture',)
class TournyRegForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('reg_game_choice', 'user_tag',)
查看:
@login_required
def tourny_reg(request):
#Registering for tournaments
context_dict = {}
weekday = datetime.datetime.today().weekday()
day_names = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY']
game_days = ['SMASH MELEE', 'SMASH 4', 'CLOSED', 'PROJECT M & FIGHTING GAMES',
'FRIENDLIES', 'CLOSED', 'CLOSED']
day_title = day_names[weekday]
game_day = game_days[weekday]
context_dict['day'] = day_title
context_dict['game_of_the_day'] = game_day
if request.method == 'POST':
tourny_form = TournyRegForm(data=request.POST)
if tourny_form.is_valid():
tourny_form.save()
else:
print (tourny_form.errors)
else:
tourny_form = TournyRegForm()
context_dict['tourny_form'] = tourny_form
return render(request, 'Kappa/tourny_reg.html', context_dict)
它在html和本地服務器上顯示得非常好,但是當我嘗試時,它會給我一個完整性錯誤。
IntegrityError在/卡帕/ tourny_reg/
NOT NULL約束失敗:Kappa_userprofile.user_id 異常值:
NOT NULL約束失敗:Kappa_userprofile.user_id ▶本地變量 C:\用戶\凱爾\ Documents \ GitHub \ Kappa_Ranks \ Kappa \ views.py在tourny_reg
這是什麼魔術......它的工作原理。 –