【问题标题】:How to Insert & Update Multiple Record in Django如何在 Django 中插入和更新多条记录
【发布时间】:2020-06-02 06:45:31
【问题描述】:

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

现在,问题是如何将此表单数据插入产品特征表(问题开头的产品特征模型)。

我想在产品功能表中插入多条记录,每条记录都有它的 feature_id、product_id 和 product_feature 值。

我真的很困惑如何做到这一点,我是 Django 开发的新手。

【问题讨论】:

    标签: mysql django django-models django-views django-mysql


    【解决方案1】:

    您可以使用默认添加到您的网址的 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 创建超级用户 以及您的应用在已安装的应用中

    对于裁剪的用户界面,您需要有另一个表单来获取数据并使用 request.body 作为 json 将其提供给支持

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

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

    如果只是为你的模型创建对象,请使用这个

    obj = MyModel.objects.create(value)
    

    【讨论】:

    • 不!但我没有使用 Django 默认管理面板我创建了我的自定义管理面板我的问题有解决方案吗?
    • 是的,您可以将提交给后端的值作为字典读取并为您的模型创建对象
    • 如果有请给个链接?
    • 您需要让视图获取并验证您的表单并保存!如果它回答了您的问题,请将其标记为答案
    【解决方案2】:

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

    【讨论】:

    • 谢谢你,但你可以检查我的问题我使用普通或自定义表单不使用 Django 表单,所以你有另一种解决方案吗?
    • 您仍然可以将自定义 HTML 与服务器端使用的 Django Form 或 FormSet 类一起使用。这在此处的表单文档中有所介绍:docs.djangoproject.com/en/3.0/topics/forms/…
    猜你喜欢
    • 2017-04-06
    • 2015-10-04
    • 2016-08-15
    • 1970-01-01
    • 2018-05-28
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多