0

我想知道如何处理这个任务。以下是详细信息:这是 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 开发的新手。

4

2 回答 2

0

您可以使用默认添加到您的 url 的 django 管理员在 admin.py 注册模型中,您可以使用默认 ui

from django.contrib import admin

 # Register your models here.
 from medicine.models import Address,Shop


 class ShopAdmin(admin.ModelAdmin):
  list_display = ('name', 'phone', 'address')


 class AddressAdmin(admin.ModelAdmin):
  list_display = ('line_1', 'line_2', 'city', 'state', 'lat', 'lng')






 admin.site.register(Shop, ShopAdmin)
 admin.site.register(Address, AddressAdmin)

./manage.py 进行迁移 ./manage.py createsuperuser 和你的应用程序在 insatlled 应用程序中

对于 cutsomized ui,您需要另一种形式来获取数据并使用 request.body 作为 json 将其提供给支持

最佳实践是使用 django rest 框架,您可以在其中定义端点并使用序列化器,它提供了以 json/xml 进行通信的任何可支持格式

对于插入数据,将所有内容作为查询参数或有效负载主体

只需为您的模型创建对象,请使用此

obj = MyModel.objects.create(value)
于 2020-02-18T10:02:17.810 回答
0

我认为您正在寻找的是一种ModelFormSet特征形式。这允许您为多个对象复制一个表单。该文档是一个很好的入口:https ://docs.djangoproject.com/en/3.0/topics/forms/formsets/

于 2020-02-19T08:23:10.960 回答