- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了 Django 的问题,我试图在 MySQL 数据库中插入一个新行,而不为未指定的数据提供 NULL。
例如,考虑以下内容:
class MyModel(models.Model):
class Meta:
db_table = 'model_table'
managed = False
a = models.AutoField(primary_key=True)
b = models.CharField(max_length=255)
c = models.CharField(max_length=255, blank=True)
为 save() 向序列化程序提供以下数据时:
data = list()
data['b'] = 'Some Text'
serializer = serializers.MyModelSerializer(data=data)
serializer.save()
对于每个未指定的字段,生成的结果 SQL 都为 NULL,在本例中为“c”。数据库中的表不允许 NULL,但有默认值来处理未指定的数据。
是否可以重写此序列化程序功能以允许在尝试插入时完全省略字段,或者我是否必须创建一个省略这些字段的新模型并为这种特定情况创建一个“创建”模型对这些领域有了解吗?
注意:Serializer 在其 fields
变量中只有字段“b”,因此它不会立即知道“c”列。
编辑:尝试澄清一点,这是生成的 SQL,由于列中不允许空值,它失败了。但是,“c”确实有默认值。
INSERT INTO `model_table` (`b`, `c`) VALUES ('Some Text', NULL)
PK 的 'a' 的 AutoField 没问题,这在默认情况下被省略,但 'c' 被替换为 NULL,因为在提供给序列化程序的数据中没有提供任何值,而不是从完全是SQL。作为一种解决方法,我复制了模型并从中删除了不需要的字段以使其正常工作,并且生成的 SQL 是有效的,因为模型本身不了解列,但这看起来不像最好的方法来做到这一点。以下是我希望此模型和序列化程序组合输出的内容
INSERT INTO `model_table` (`b`) VALUES ('Some Text')
这样 'c' 被提供为 MySQL 本身提供的默认列值。当我没有向序列化程序提供任何数据时,模型和序列化程序尝试插入 NULL 是问题所在。
最佳答案
您可以像下面这样重写序列化器创建方法
class UserSerializer(serializers.ModelSerializer):
profile = ProfileSerializer()
class Meta:
model = User
fields = ('username', 'email', 'profile')
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create(**validated_data)
Profile.objects.create(user=user, **profile_data)
return user
关于python - Django (DRF) 序列化程序插入 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397352/
背景 我有一个定义了多个自定义操作的 ModelViewSet。我在 urls.py 中使用默认路由器注册 URL。现在,我的 View 使用默认创建的路由,如 ^images/$ , ^images
DRF 中的 request.data 和 DRF 中的 serializers.data 有什么区别。 当我在 DRF 中编写基于函数的 View 时,我会像使用它们一样 - eli
我用Django REST框架准备了API。我正在使用docker运行我的API,一切在我的机器上都运行得很好。因此,我只需运行docker-compose up,就可以在浏览器中使用Swagger测
我用Django REST框架准备了API。我正在使用docker运行我的API,一切在我的机器上都运行得很好。因此,我只需运行docker-compose up,就可以在浏览器中使用Swagger测
快速上手JWT签发token和认证,有这一篇就够了,DRF自带的和自定义的都帮你总结好了,拿去用~ DRF JWT认证(二) 上篇中对JWT有了基本的认知,这篇来略谈JWT的使用 签发:一般我们登录成
什么接口文档都不用手写了?自动挡? DRF 自动生成接口文档 我们知道前后端分离,前端一般按后端写好的接口去开发,那么就需要我们明细后端接口数据等,需要写接口文档,前端按照接口文档去开发 接口文档如何
为什么要使用JWT认证?构成和原理又是什么?怎么还有Base64的事?我都写了👆 DRF JWT认证(一) JWT认证 Json web token (JWT), 是为了在网络应用环境间传递声明而执
DRF 中如何使用过滤,排序,分页,以及报错了如何处理?10分钟get了~ DRF 过滤排序分页异常处理 过滤 涉及到查询数据的接口才需要过滤功能 DRF过滤使用种类: 内置过滤类 第三方 自定义 内
前后端分离开发怎么给接口做认证权限频率三大认证?点进来5分钟get! DRF-认证权限频率 前后端混合开发,可以通过HttpResponse对象来设置cookie进而校验登录,现在前后端分离开发,用不
你问我使用DRF视图组件快速写接口?九九归一剑诀早就准备好了,没有副作用! DRF 视图组件 DRF框架提供了很多通用的视图基类与扩展类,上篇使用的APIView是比较偏Base的,视图的使用更加简化
1、路由Routers 在 Rest Framework 中提供了两个 router , 可以帮助我们快速的实现路由的自动生成。 必须是继承 ModelViewSet 的视图类才能自动生成路由
1. 前言 大家好,我是安果! 接下来,将通过几篇文章将对 DRF 模型序列化进行展开说明,详情谈谈模型序列化的作用、步骤及进阶用法 2. 模型序列化的作用?  
我第一次尝试进入 Django 原子事务。我想知道是否有可能像这样使用它: class TaskViewSet(MultiSerializerViewSet): @transaction.at
我有一个 drf 序列化程序,其中有一个我想重命名的字段: class UserBulkUploadSerializer(serializers.Serializer): ... is
我使用了 drf 并且还使用了 serializer 来检查用户输入。 这是我的serializer.py class BoardSerializer(serializers.Serializer):
我正在尝试序列化一个 CreateUserSerializer(ModelSerializer)我的代码如下。我的问题是它只会创建一个 User 而不是 UserProfile。 模型.py clas
我如何发布这个 JSON { "campaign": 27, "campaignName": "Prueba promo", "promotionType": 999, "items"
我正在构建一个 todo 应用程序,可以在 todo 上发布评论。 为此,我制作了一个可写的双嵌套序列化程序。它有效,但我编写的更新方法难以理解,因此我试图使其更具可读性。是否有更好的(或标准的)方法
有没有办法为特定的 View 类指定所需的自定义 header ?在我正在使用的 API 上,一些信息是使用 header 传递的,如果缺少 header ,我需要返回 BAD_REQUEST 或类似
我正在使用 DRF 并且我正在尝试创建一个对象,该对象具有多个外键以及需要在此过程中创建的相关对象。 这是我的模型的简化版本: class Race(models.Model): name =
我是一名优秀的程序员,十分优秀!