当前位置: 首页 > news >正文

郑州网站建设哪家公司好做网站交接什么时需要交接

郑州网站建设哪家公司好,做网站交接什么时需要交接,中国采购网招标公告,做推广哪个网站最好DRF的ModelSerializer序列化器与Django的Model模型紧密映射#xff0c;本文将通过简单的示例介绍几种处理关联关系的方法。 1. 创建模型和初始数据 创建模型 from django.db import modelsclass Product(models.Model):product_name models.CharField(max_length255)quant…DRF的ModelSerializer序列化器与Django的Model模型紧密映射本文将通过简单的示例介绍几种处理关联关系的方法。 1. 创建模型和初始数据 创建模型 from django.db import modelsclass Product(models.Model):product_name models.CharField(max_length255)quantity models.IntegerField()class Component(models.Model):product models.ForeignKey(Product, on_deletemodels.CASCADE, related_namecomponents)component_name models.CharField(max_length255)code models.CharField(max_length255)quantity models.IntegerField()class Meta:ordering [product]def __str__(self):return f{self.code}-{self.component_name}创建数据 from api.models import Product, Componentp1 Product.objects.create(product_name盖子, quantity30)Component.objects.create(productp1, component_name纤维板, codeXYB, quantity5)Component.objects.create(productp1, component_name螺丝, codeLS, quantity10)Component.objects.create(productp1, component_name密封条, codeMFT, quantity20)2. 字符串关系字段 使用StringRelatedField from rest_framework import serializers from .models import Productclass ProductSerializer(serializers.ModelSerializer):components serializers.StringRelatedField(manyTrue)class Meta:model Productfields [product_name, quantity, components]执行查询和序列化 from api.models import Productp1 Product.objects.prefetch_related(components).get(pk1)from api.serializers import ProductSerializerps ProductSerializer(p1)ps.data # {product_name: 盖子, quantity: 30, components: [XYB-纤维板, LS-螺丝, MFT-密封条]}效果如下: {product_name: 盖子, quantity: 30, components: [XYB-纤维板, LS-螺丝, MFT-密封条] }注意观察获取查询集的代码:Product.objects.prefetch_related(components).get(pk1)不难发现我们使用了prefetch_related来获取查询集如果一次查询大量的Product不使用prefetch_related将会导致严重的性能问题N1问题。DRF的序列化器不会为你自动优化。当然我们的示例中影响不大因为只获取了一个查询集。 3. 主键关系字段 使用PrimaryKeyRelatedField from rest_framework import serializers from .models import Productclass ProductSerializer(serializers.ModelSerializer):components serializers.PrimaryKeyRelatedField(manyTrue, read_onlyTrue)class Meta:model Productfields [product_name, quantity, components]效果如下 {product_name: 盖子, quantity: 30, components: [1, 2, 3] }4. 指定关系字段 使用SlugRelatedField可以指定字段来表示关联关系 from rest_framework import serializers from .models import Productclass ProductSerializer(serializers.ModelSerializer):components serializers.SlugRelatedField(manyTrue,read_onlyTrue,slug_fieldcode)class Meta:model Productfields [product_name, quantity, components]效果如下 {product_name: 盖子, quantity: 30, components: [XYB, LS, MFT] }5. 嵌套序列化器 from rest_framework import serializers from .models import Product, Componentclass ComponentSerializer(serializers.ModelSerializer):class Meta:model Componentfields [component_name, code, quantity]class ProductSerializer(serializers.ModelSerializer):components ComponentSerializer(manyTrue, read_onlyTrue)class Meta:model Productfields [product_name, quantity, components] 效果如下 {product_name: 盖子,quantity: 30,components: [{component_name: 纤维板,code: XYB,quantity: 5},{component_name: 螺丝,code: LS,quantity: 10},{component_name: 密封条,code: MFT,quantity: 20}] }6. 可写嵌套 默认上面创建的嵌套序列化器是只读的可写嵌套需要实现update、create两者或其一 from rest_framework import serializers from .models import Product, Componentclass ComponentSerializer(serializers.ModelSerializer):class Meta:model Componentfields [component_name, code, quantity]class ProductSerializer(serializers.ModelSerializer):components ComponentSerializer(manyTrue)class Meta:model Productfields [product_name, quantity, components]def create(self, validated_data):components_data validated_data.pop(components)product Product.objects.create(**validated_data)for component_data in components_data:Component.objects.create(productproduct, **component_data)return product 可以使用此序列化器创建实例 data {product_name: 电机,quantity: 1,components: [{component_name: 铝型材,code: LXC,quantity: 5},{component_name: 螺栓,code: LSHUN,quantity: 10},] }from api.serializers import ProductSerializers ProductSerializer(datadata)s.is_valid() # Trues.save() # Product: Product object (2)7. 总结注意事项 关联关系注意使用prefetch_related获取查询集。可写嵌套序列化器必须自己实现create方法update方法两者或其一。
http://www.hkea.cn/news/14427455/

相关文章:

  • 精品网的功能和服务网站大图片优化
  • 详情页制作模板seo 服务
  • .net网站开发中注册页面凡科快图官网下载
  • 哪些网站用.ren域名电子商务平台名词解释
  • 网站开发主要职责找回网站后台
  • 网站建设阐述网页和网站区别是什么
  • 怎么做网站的访问量网站开发及维护招聘
  • 吉林省软环境建设网站花房姑娘直播
  • 咸宁网站定制成都旅游必去景点有哪些
  • 淘客做的领券网站php能开发大型网站
  • 网站程序上传工具呼和浩特房地产网站建设
  • 首钢建设二公司网站郑州网站优化公司
  • 如何更快的学习.net网站开发网站建设业务前景
  • 网站建设明细标价表建网站有多少种方式
  • asp.net mvc 5网站开发之美 pdf网站后台内容不更新
  • 海口网站制作计划手机排名
  • 网站婚庆模板网站排名提升易下拉教程
  • 汉中微信网站建设推广中型网站流量
  • 要网站开发费用短信秀堂h5官网
  • 呼和浩特哪里做网站织梦网站模板源码下载
  • 建湖做网站哪家最好建设银行网银盾不能打开网站
  • 头像制作免费重庆网站搜索引擎seo
  • 做简历用什么网站html编辑工具有哪些
  • 网站开发工具cwordpress淘宝客插件开发
  • 网站锚文本使用查询密云区社区建设网站
  • wordpress导入网站国外服务器租用网站
  • 免费网上销售平台吉林seo排名公司
  • 杭州专业做网站的公司哪家好进地铁建设公司网站
  • 简述网站建设的具体步骤网站运营一般做那些分析
  • 淮南二中网站建设网盘wordpress