如何在 django 中保存模型时自动保存用户名而不是对象名(None)
How to save Username instead of Object name(None) automatically while saving the model in django
check the updated pic
当我保存模型时,对象名称是None,但我需要在保存模型时自动保存用户名而不是对象名称(None)
here is pic of admin site
Models.py
class solo_21_6_2021(models.Model):
user = models.OneToOneField(User,null=True,on_delete=models.CASCADE)
player1_pubg_id = models.PositiveIntegerField(null=True,blank=True)
player1_pubg_name = models.CharField(max_length=15,null=True,blank=True)
def __str__(self):
return str(self.user)
Views.py
def solo(request):
form = SoloForm()
if request.method=="POST":
form=SoloForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'bgmiapp/solo.html',{'form':form})
Forms.py
class SoloForm(forms.ModelForm):
class Meta():
model = solo_21_6_2021
fields=['player1_pubg_id','player1_pubg_name'
Admin.py
class SoloAdmin(admin.ModelAdmin):
list = ('user','player1_pubg_id','player1_pubg_name')
admin.site.register(Profile)
admin.site.register(solo_21_6_2021,SoloAdmin)
您已将用户字段设置为空,因此创建模型时没有它,因此当您想要按用户显示对象时,它没有此信息,因此显示 None。
将模型修改为:
class solo_21_6_2021(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
player1_pubg_id = models.PositiveIntegerField(null=True,blank=True)
player1_pubg_name = models.CharField(max_length=15,null=True,blank=True)
def __str__(self):
return str(self.user)
所以现在它是必需的和相关的,因为它应该是
和inf表格:
class SoloForm(forms.ModelForm):
class Meta():
model = solo_21_6_2021
fields=['user','player1_pubg_id','player1_pubg_name']
所以它以html形式显示用户的选择
现在当您提交表单时,用户名将显示在管理页面
*或者您可以让经过身份验证的用户成为创建对象的用户,这样就无需向所有人显示您的用户列表:
def solo(request):
form = SoloForm()
if request.method=="POST":
form=SoloForm(request.POST)
if form.is_valid():
f = form.save(commit=False)
f.user = request.user
f.save()
return render(request, 'bgmiapp/solo.html',{'form':form})
并删除表单中的用户:
class SoloForm(forms.ModelForm):
class Meta():
model = solo_21_6_2021
fields=['player1_pubg_id','player1_pubg_name']
Ps: 因为你有 OnetoOne 关系,你只能由用户创建一个对象,创建另一个将抛出 user_id 唯一约束错误
check the updated pic
当我保存模型时,对象名称是None,但我需要在保存模型时自动保存用户名而不是对象名称(None)
here is pic of admin site
Models.py
class solo_21_6_2021(models.Model):
user = models.OneToOneField(User,null=True,on_delete=models.CASCADE)
player1_pubg_id = models.PositiveIntegerField(null=True,blank=True)
player1_pubg_name = models.CharField(max_length=15,null=True,blank=True)
def __str__(self):
return str(self.user)
Views.py
def solo(request):
form = SoloForm()
if request.method=="POST":
form=SoloForm(request.POST)
if form.is_valid():
form.save()
return render(request, 'bgmiapp/solo.html',{'form':form})
Forms.py
class SoloForm(forms.ModelForm):
class Meta():
model = solo_21_6_2021
fields=['player1_pubg_id','player1_pubg_name'
Admin.py
class SoloAdmin(admin.ModelAdmin):
list = ('user','player1_pubg_id','player1_pubg_name')
admin.site.register(Profile)
admin.site.register(solo_21_6_2021,SoloAdmin)
您已将用户字段设置为空,因此创建模型时没有它,因此当您想要按用户显示对象时,它没有此信息,因此显示 None。
将模型修改为:
class solo_21_6_2021(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
player1_pubg_id = models.PositiveIntegerField(null=True,blank=True)
player1_pubg_name = models.CharField(max_length=15,null=True,blank=True)
def __str__(self):
return str(self.user)
所以现在它是必需的和相关的,因为它应该是
和inf表格:
class SoloForm(forms.ModelForm):
class Meta():
model = solo_21_6_2021
fields=['user','player1_pubg_id','player1_pubg_name']
所以它以html形式显示用户的选择
现在当您提交表单时,用户名将显示在管理页面
*或者您可以让经过身份验证的用户成为创建对象的用户,这样就无需向所有人显示您的用户列表:
def solo(request):
form = SoloForm()
if request.method=="POST":
form=SoloForm(request.POST)
if form.is_valid():
f = form.save(commit=False)
f.user = request.user
f.save()
return render(request, 'bgmiapp/solo.html',{'form':form})
并删除表单中的用户:
class SoloForm(forms.ModelForm):
class Meta():
model = solo_21_6_2021
fields=['player1_pubg_id','player1_pubg_name']
Ps: 因为你有 OnetoOne 关系,你只能由用户创建一个对象,创建另一个将抛出 user_id 唯一约束错误