如何解决用户添加的Django模型额外字段
该模型将要求输入人员姓名和资格,这都是文本字段,但人员数量因实例而异。我如何让用户向模型添加字段?
这是我目前的模特:
class ShiftReport(models.Model):
work = models.ForeignKey(Work,null=True,on_delete=models.CASCADE)
work_description = models.TextField(max_length=500)
handover = models.BooleanField(default=False)
weather_conditions = models.TextField(max_length=100)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
noms_number = models.TextField(max_length=40)
noms_phase_addition_number = models.TextField(max_length=5)
personnel_name = models.TextField(max_length=100)
personnel_qualifications = models.TextField(max_length=100)
personnel_name_2 = models.TextField(max_length=100)
personnel_qualifications_2 = models.TextField(max_length=100)
我希望能够让用户添加他们想要的尽可能多的人员姓名和资格。
class ShiftReport(models.Model):
work = models.ForeignKey(Work,on_delete=models.CASCADE)
work_description = models.TextField(max_length=500)
handover = models.BooleanField(default=False)
weather_conditions = models.TextField(max_length=100)
start_time = models.DateTimeField()
end_time = models.DateTimeField()
noms_number = models.TextField(max_length=40)
noms_phase_addition_number = models.TextField(max_length=5)
personnel_name = models.TextField(max_length=100)
personnel_qualifications = models.TextField(max_length=100)
personnel_count = 1
def add_personnel(self):
self.personnel_count += 1
ADDFIELD(personnel_name_{self.personnel_count})
ADDFIELD(personnel_qualifications_{self.personnel_count}
只是不太确定如何实现此目的。救命!
解决方法
不可能使用户动态地向模型添加字段。但是您可以做的是,制作一个容器模型和您要存储的内容模型方法。
例如,(摘录自我的代码)。我想存储用户可以发送给我们的第 n 个文档。而不是静态定义字段 10次。我宁愿创建一个可以包含一组文档的文档容器。
容器模型= SupportDocsContainer
ContentToStoreToContainer模型= SupportDocs
# ! ContentToStoreToContainer Model
class SupportDocs(models.Model):
document = models.FileField(
upload_to=<...>,null=False,blank=False,max_length=2048,verbose_name="Case-Supporting Documents / Report Proofs",help_text="Images or documents that can be submitted inside Review Support Case.",validators=[
FileExtensionValidator(
allowed_extensions=(["pdf","docx","doc","jpg","jpeg","png","gif"])
)
],)
# ! Container Model
class SupportDocsContainer(models.Model):
documents = models.ManyToManyField(
"SupportDocs",editable=True,verbose_name="List of Documents Provided by the Message Sender / Request Initiator",help_text="A many-to-many field that involves documents.",)
dtLastModified = models.DateTimeField(
auto_now=True,editable=False,verbose_name="Date and Time Document Container was Modified",help_text="The date and time from where the document container has been modified.",)
dtCreated = models.DateTimeField(
auto_now_add=True,verbose_name="Date and Time Document Container was Created.",help_text="The date and time from where the document container has been created.",)
使用这种方法,您可以使ShiftReport
对象创建一个对象( 通过信号,清除或保存方法自动进行。(但可以自行实现...) ),用户可以从中保存特定数据。
一种方法是,将人员姓名和资格字段设置为ShiftReport
的ForeignKey实例,然后使用javascript进行动态添加。
,因为您尚未共享任何其他数据。因此,为了给您一个想法,假设模型如下:
class ShiftReport(models.Model):
work_description = models.TextField(max_length=500)
class PersonalInfo(models.Model):
shift_report = models.ForeignKey(ShiftReport,on_delete=models.CASCADE)
personnel_name = models.TextField(max_length=100)
personnel_qualifications = models.TextField(max_length=100)
那么视图将是:
def shift_report_view(request):
if request.method == "POST":
work_description = request.POST.get("work_description")
personnel_name_list = request.POST.getlist("personnel_name")
personnel_qualifications_list = request.POST.getlist("personnel_qualifications")
personnel_info = zip(personnel_name_list,personnel_qualifications_list)
shift_report_data = ShiftReport(work_description=work_description)
shift_report_data.save()
for i in personnel_info:
if not len(i[0]) == len(i[1]) == 0:
personal_info_data = PersonalInfo(shift_report=shift_report_data,personnel_name=i[0],personnel_qualifications=i[1])
personal_info_data.save()
return render(request,"shift_report.html")
和在模板中:
<form method="POST" action="">
{% csrf_token %}
<input type="text" name="work_description" placeholder="Work Description"><br><br>
<div id="personalInfo">
<input type="text" name="personnel_name" placeholder="Personnel Name"><br><br>
<input type="text" name="personnel_qualifications" placeholder="Personnel Qualifications"><br><br>
</div>
<input type="button" id="addPersonalInfo" value="Add Personal Info">
<input type="submit" value="submit">
</form>
<script type="text/javascript">
document.getElementById("addPersonalInfo").onclick = function() {
var personnelName = document.createElement("input");
personnelName.type = "text";
personnelName.name = "personnel_name";
personnelName.placeholder = "Personnel Name";
var personnelQualifications = document.createElement("input");
personnelQualifications.type = "text";
personnelQualifications.name = "personnel_qualifications";
personnelQualifications.placeholder = "Personnel Qualifications";
var personnelDiv = document.getElementById("personalInfo");
personnelDiv.appendChild(personnelName);
personnelDiv.appendChild(document.createElement("br"));
personnelDiv.appendChild(document.createElement("br"));
personnelDiv.appendChild(personnelQualifications);
personnelDiv.appendChild(document.createElement("br"));
personnelDiv.appendChild(document.createElement("br"));
}
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。