我想知道如何处理这个任务。以下是详细信息:这是 product_feature 模型
class Feature(models.Model):
has_value_choice = [
('y', 'Yes'),
('n', 'No'),
]
feature_id = models.AutoField(primary_key=True)
feature_name = models.CharField(max_length=255, null = False)
product_type = models.ForeignKey(Product_type, on_delete=models.SET_NULL, null=True)
feature_has_value = models.CharField(choices=has_value_choice, max_length = 1, null = False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
class Product_feature(models.Model):
product_feature_id = models.AutoField(primary_key=True)
feature = models.ForeignKey(Feature, on_delete=models.SET_NULL, null=True)
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
product_feature_value = models.CharField(max_length=255, null = False)
created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)
在这里,我将特征数据传递给模板,以将特定产品的特征保存在 product_feature 模型中。
<form>
<table class="table table-bordered">
<thead>
<tr>
<th>Features</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for data in features_data %}
<tr>
{% if data.feature_has_value != 'n' %}
<td>{{ data.feature_name }}</td>
<td><input type="text" name="{{ data.feature_name }}" id="{{ data.feature_name }}" placeholder="Enter {{ data.feature_name }}" class="form-control" required/></td>
{% else %}
<td>{{ data.feature_name }}</td>
<td>
<select id="{{ data.feature_name }}" name="{{ data.feature_name }}" class="form-control" required>
<option selected id="select" value="yes">Yes</option>
<option value="no">No</option>
</select>
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</form>
现在,问题是如何将此表单数据插入产品特征表(问题开头的 product_feature 模型)。
我想在产品功能表中插入多条记录,每条记录都有它的 feature_id、product_id 和 product_feature 值。
我真的很困惑如何做到这一点我是 Django 开发的新手。