我有问题多次保存我的表单。我希望用户用尽可能多的维度填写我的表单(这是表单)所以,如果他们这样做了一次只将一条记录插入到我的数据库中,如果他们做了两个维度,将每个记录保存到我的数据库总共两个新记录。任何帮助解决这个问题将不胜感激。
这是我的ajax
function SaveDim() {
$.ajax({
type: "POST",
url: "/sheet/sheet_form_create.html/_dim",
//dataType: "json",
data: $('#dim_form').serialize()
+
"&description=" + $('#id_description').val() +
"&style=" + $('#id_style').val() +
"&target=" + $('#id_target').val() +
"&upper_limit=" + $('#id_upper_limit').val() +
"&lower_limit=" + $('#id_lower_limit').val() +
"&inspection_tool=" + $('#id_inspection_tool').val() +
"&critical=" + $('#id_critical').val() +
"&units=" + $('#id_units').val() +
"&metric=" + $('#id_metric').val() +
"&target_strings=" + $('#id_target_strings').val() +
"&ref_dim_id=" + $('#id_ref_dim_id').val() +
"&nested_number=" + $('#id_nested_number').val() +
"&posistion=" + $('#id_position').val() +
"&met_upper=" + $('#id_met_upper').val() +
"&met_lower=" + $('#id_met_lower').val() +
"&valc=" + $('#id_valc').val() +
"&sheet_id=" + $('#id_sheet_id').val() +
"",
success: function (json) {
console.log(json);
alert(json);
}
});
}
这是我的views.py(add_dimension)方法
def add_dimensions(request):
if request.method == 'POST':
c_date = datetime.now()
u_date = datetime.now()
description = request.POST.get('description')
style = request.POST.get('style')
target = request.POST.get('target')
upper_limit = request.POST.get('upper_limit')
lower_limit = request.POST.get('lower_limit')
inspection_tool = request.POST.get('inspection_tool')
critical = request.POST.get('critical')
units = request.POST.get('units')
metric = request.POST.get('metric')
target_strings = request.POST.get('target_strings')
ref_dim_id = request.POST.get('ref_dim_id')
nested_number = request.POST.get('nested_number')
met_upper = request.POST.get('met_upper')
met_lower = request.POST.get('met_lower')
valc = request.POST.get('valc')
sheet_id = request.POST.get('sheet_id')
data = {}
dim = Dimension(
description=description,
style=style,
target=target,
upper_limit=upper_limit,
lower_limit=lower_limit,
inspection_tool=inspection_tool,
critical=critical,
units=units,
metric=metric,
target_strings=target_strings,
ref_dim_id=ref_dim_id,
nested_number=nested_number,
met_upper=met_upper,
met_lower=met_lower,
valc=valc,
sheet_id=sheet_id,
created_at=c_date,
updated_at=u_date)
dim.save()
data['description'] = dim.description;
data['style'] = dim.style;
data['target'] = dim.target;
data['upper_limit'] = dim.upper_limit;
data['lower_limit'] = dim.lower_limit;
data['inspection_tool'] = dim.inspection_tool;
data['critical'] = dim.critical;
data['units'] = dim.units;
data['metric'] = dim.metric;
data['target_strings'] = dim.target_strings;
data['ref_dim_id'] = dim.ref_dim_id;
data['nested_number'] = dim.nested_number;
data['met_upper'] = dim.met_upper;
data['met_lower'] = dim.met_lower;
data['valc'] = dim.valc;
data['sheet_id'] = dim.sheet_id;
return HttpResponse(json.dumps(data), content_type="application/json",)
else:
dim_form = DimForm()
return render(request, 'app/_dim.html', {'dim_form': dim_form})
模型
class Dimension(models.Model):
description = models.CharField(max_length=255)
style = models.CharField(max_length=255)
created_at = models.DateField()
updated_at = models.DateField()
target = models.IntegerField()
upper_limit = models.IntegerField()
lower_limit = models.IntegerField()
inspection_tool = models.CharField(max_length=255)
critical = models.IntegerField()
units = models.CharField(max_length=255)
metric = models.CharField(max_length=255)
target_strings = models.CharField(max_length=255)
ref_dim_id = models.IntegerField()
nested_number = models.IntegerField()
#position = models.IntegerField()
met_upper = models.IntegerField()
met_lower = models.IntegerField()
valc = models.CharField(max_length=255)
sheet = models.ForeignKey(Sheet, on_delete=models.CASCADE, default=DEFAULT_FOREIGN_KEY)
答案 0 :(得分:1)
将更改telnet 10.106.138.210 9010
更改为dim.save()
https://stackoverflow.com/a/23926742/475565
你也应该将id改为类
Dimension.objects.create()