javascript - 如何将相关数据返回到外键 django ajax
问题描述
我有一个项目,有两种型号,一种用于计算机功能,另一种用于计算机发票
class ComputerFeature(models.Model):
car_model = models.CharField(max_length=50)
CPU = models.CharField(max_length=30)
RAM = models.CharField(max_length=30)
#others
class CustomerInvoice(models.Model):
customer = models.CharField(max_length=50)
model = models.ForeignKey(ComputerFeature, on_delete=models.CASCADE)
order = models.IntegerField(default=1)
price = models.IntegerField()
这是我的模板
<form method='POST'>{% csrf_token %}
<table class="table">
<thead>
<tr>
{{ form.customer.errors }}
<th class="float-left" id="customer">Customer : {{ form.customer }}</th>
</tr>
</thead>
</table>
<table class="table">
<thead>
<tr>
<td><div class="form-group floating-label" id="modeling">
{{ form.model | add_class:'form-control select2-list ' | attr:'id:model'}}
</div></td>
</tr>
<tr>
{{ form.order.errors }}
<td scope="col">quantity</td>
<td scope="col" id="order">{{form.order}}</td>
</tr>
<tr>
<td scope="col">CPU</td>
<td scope="col" style="width: 50%" id="cpu">
<--! i want to return CPU here before submiting the form -->
</tr>
<tr>
<td scope="col">RAM</td>
<--! i want to return CPU here before submiting the form -->
</tr>
<tr>
{{ form.Hard.errors }}
<td scope="col">Hard</td>
<td scope="col" id="hard">{{form.model.Hard}}</td>
</tr>
<tr>
<td scope="col">Graphic</td>
<--! i want to return CPU here before submiting the form -->
</tr>
<tr>
{{ form.price.errors }}
<td scope="col">price/td>
<td scope="col" id="price">{{form.price}}</td>
</tr>
</thead>
</table>
<input type="submit" name="save" value="selling" class="btn btn-primary btn-block" id="submit">
</div>
</div>
</form>
我需要在提交表单之前从 ComputerFeature 返回 CPU、RAM 和其他功能,还记得吗?
这是我的表单类
class CustomerInvoice(forms.ModelForm):
model = forms.ModelChoiceField(queryset=ComputerFeature.objects.all().order_by('-date'),empty_label='models')
class Meta:
model = CustomerInvoice
fields = ['customer','model','price','order']
我知道我应该使用 ajax 请求,但我不知道如何实现它!
谢谢你的帮助
解决方案
推荐阅读
- javascript - 如何在页面加载时生成随机数?
- java - HashMap get 方法在 hm.get(hm) 上抛出错误
- php - 试图获取非对象的属性“标题”(查看:/opt/lampp/htdocs/commonroom/resources/views/home.blade.php
- node.js - 使用 Axios/Axios-retry 向服务器发送数据的 HTTP 调用
- selenium - 如何处理 Cucumber AmbiguousStepDefinitions 异常?
- javascript - 在phantomjs中读取本地文件
- database - 为什么 cassandra 导入模式较慢
- reactjs - 使用 AWS 后端通过 Next.js/React 登录 JWT
- .htaccess - 更改浏览器 URL,而不是实际路径或文件位置
- vb.net - 模块真的与 SharedMembers-NotInheritable-PrivateNew 类相同吗?