我在html上创建了带有动态增加的文本字段的表单。如何在django models.py中将来自文本字段的所有输入存储在单个'collaborators'属性下?
这是我的html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var i=1;
$('#addCollab').click(function(){
i++;
$('#dynamicField').append('<tr id="row'+i+'"><td><input type="text" name="collab" id="collab'+i+'"></td><td><button type="button" name="removeCollab" id="'+i+'" class="removeCollab">x</button></td></tr>')
});
$(document).on('click', '.removeCollab', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
});
</script>
</head>
<body>
<div class="formGrp">
<form>
<div class="collaborators">
<table id="dynamicField">
<tr>
<td>
<label for="collabID">Collaborator Names: </label>
<input type="text" name="collab" id="collab1">
</td>
<td><button type="button" name="addCollab" id="addCollab">Add Collaborator</button></td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
</div>
</form>
</div>
</body>
</html>
python代码:
class projectDetails(models.Model):
project_id=models.BigAutoField(primary_key=True)
#collaborators=
date_created=models.DateTimeField(auto_add_now=True)
答案 0 :(得分:0)
您可以将其作为json数据存储在Django模型中。这是将处理
的代码from django.contrib.postgres.fields import JSONField
class projectDetails(models.Model):
project_id=models.BigAutoField(primary_key=True)
collaborators= JSONField()
date_created=models.DateTimeField(auto_add_now=True)
与django> 1.9和postgres一起使用