我有一个系统,用户可以在公司页面上发表评论 我创建的视图的更多信息不保存表单 保存表单时,我需要它带有公司资料页面的ID和用户的ID或名称。
故事: 用户输入公司X的个人资料,进行评论并键入公司的评估号,当您保存此表单时,我需要您拥有FK的公司ID和用户系统的UserSystem的名称或ID。也是fk
models.py
class Company(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
comapany = models.CharField(max_length=50, blank=False)
class UserSystema(models.Model):
name = models.CharField(max_length=50, blank=False)
lastname = models.CharField(max_length=50, blank=False)
class Comments(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.ForeignKey(Usuario, on_delete=models.CASCADE)
comments = models.CharField(max_length=255, blank=True)
number = models.CharField(max_length=255, choices=NOTA_CHOICES, blank=True)
views.py
def Comments(request):
if request.method == 'POST':
form = CommentsForm()
form.instance.user = request.user
form.instance.name = request.user
if form.is_valid():
form = form.save()
return redirect('system_profile')
else:
form = CommentsForm()
return render(request, 'profile/comments.html', {'form': form})