如何在Django表单中关联登录用户并展示提交数据及用户信息
实现登录用户与表单提交的关联及展示
一、修正并完善模型(Model)
首先需要在QA_Models中添加与提交用户的关联字段,同时修正Agent模型的错误写法(将角色选择从关联字段移到单独字段):
from django.db import models from django.contrib.auth.models import User class Agent(models.Model): # 修正:将角色选项放在单独字段,而非OneToOneField Role = [ ('Manager','Manager'), ('Agent','Agent'), ] QA_Agent = models.OneToOneField(User, on_delete=models.CASCADE) role = models.CharField(choices=Role, max_length=10) # 新增角色字段 def __str__(self): return self.QA_Agent.username # 其他选项常量保持不变... class QA_Models(models.Model): # 新增:关联提交表单的Agent(即登录用户) submitted_by = models.ForeignKey(Agent, on_delete=models.CASCADE, related_name='qa_submissions') # 原有字段保持不变 Policy_Number = models.CharField(max_length=12) Case_Number = models.CharField(max_length=7) AVS_Check = models.CharField(choices=AVS, max_length=12) Caller_ID = models.CharField(max_length=50) Call_duration = models.CharField(max_length=10) Start_date = models.CharField(choices=StartDate, max_length=20) Premium = models.DecimalField(max_digits=6, decimal_places=2) Debit_date = models.CharField(max_length=10) Cover_amount = models.CharField(max_length=10) QA_Correct = models.CharField(choices=QA_Outcome, max_length=5) KPA_1 = models.CharField(choices=KPA_Outcome, max_length=15, null=True) KPA_2 = models.CharField(choices=KPA_Outcome, max_length=15, null=True) KPA_3 = models.CharField(choices=KPA_Outcome, max_length=15, null=True) HIV_Required = models.CharField(choices=HIV_Test, max_length=3) Comment = models.TextField(max_length=200) def __str__(self): return self.Case_Number
二、更新表单(Form)
修改QA_Forms,排除submitted_by字段(该字段由系统自动赋值,无需用户填写):
# form.py from django import forms from .models import QA_Models class QA_Forms(forms.ModelForm): class Meta: model = QA_Models exclude = ['submitted_by'] # 排除关联字段,不显示在表单中
三、修改视图(View)
在提交表单时,自动将当前登录用户关联到QA_Models实例:
from django.shortcuts import render, redirect from .models import QA_Models, Agent from .form import QA_Forms from .decorations import allowed_user @allowed_user(allowed_roles=['Manager','Agent']) def Home(request): TheData = QA_Models.objects.all() return render(request, 'Home.html', {'TheData':TheData}) @allowed_user(allowed_roles=['Manager','Agent']) def MainPage(request): if request.method == 'POST': form = QA_Forms(request.POST) if form.is_valid(): # 获取当前登录用户对应的Agent实例 agent = Agent.objects.get(QA_Agent=request.user) # 保存表单前关联Agent qa_instance = form.save(commit=False) qa_instance.submitted_by = agent qa_instance.save() return redirect('Home') # 提交后跳转到列表页 else: form = QA_Forms() return render(request, 'MainPage.html', {'TheForm': form, 'TheUser': request.user})
四、更新模板(Template)
在列表页添加提交者用户名的展示:
{% extends "Layout.html" %} {% block content %} <h1>Home</h1> <div class="container"> <br> <table class="table table-dark table-hover"> <tr> <!-- 新增表头:提交者 --> <th>提交者</th> <th>保单号</th> <th>案件号</th> <th>QA结果</th> </tr> {% for Information in TheData %} <tr> <!-- 显示提交者用户名 --> <td>{{ Information.submitted_by.QA_Agent.username }}</td> <td>{{ Information.Policy_Number }}</td> <td> <a href="{% url 'DataInformation' Information.id %}"> {{ Information.Case_Number }} </a> </td> <td>{{ Information.QA_Correct }}</td> </tr> {% endfor %} </table> </div> {% endblock %}
最后执行数据库迁移
修改模型后,需要执行迁移命令更新数据库:
python manage.py makemigrations python manage.py migrate
内容的提问来源于stack exchange,提问作者Sizwe
相关产品推荐
相关产品推荐

