<bdo id='JCULh'></bdo><ul id='JCULh'></ul>

<small id='JCULh'></small><noframes id='JCULh'>

    1. <legend id='JCULh'><style id='JCULh'><dir id='JCULh'><q id='JCULh'></q></dir></style></legend>
      1. <tfoot id='JCULh'></tfoot>
      2. <i id='JCULh'><tr id='JCULh'><dt id='JCULh'><q id='JCULh'><span id='JCULh'><b id='JCULh'><form id='JCULh'><ins id='JCULh'></ins><ul id='JCULh'></ul><sub id='JCULh'></sub></form><legend id='JCULh'></legend><bdo id='JCULh'><pre id='JCULh'><center id='JCULh'></center></pre></bdo></b><th id='JCULh'></th></span></q></dt></tr></i><div id='JCULh'><tfoot id='JCULh'></tfoot><dl id='JCULh'><fieldset id='JCULh'></fieldset></dl></div>
      3. 固定装置的 Django 自然键给出反序列化错误

        时间:2023-09-13

          • <bdo id='DwnOH'></bdo><ul id='DwnOH'></ul>
          • <i id='DwnOH'><tr id='DwnOH'><dt id='DwnOH'><q id='DwnOH'><span id='DwnOH'><b id='DwnOH'><form id='DwnOH'><ins id='DwnOH'></ins><ul id='DwnOH'></ul><sub id='DwnOH'></sub></form><legend id='DwnOH'></legend><bdo id='DwnOH'><pre id='DwnOH'><center id='DwnOH'></center></pre></bdo></b><th id='DwnOH'></th></span></q></dt></tr></i><div id='DwnOH'><tfoot id='DwnOH'></tfoot><dl id='DwnOH'><fieldset id='DwnOH'></fieldset></dl></div>

              <tbody id='DwnOH'></tbody>
                <tfoot id='DwnOH'></tfoot>

                <legend id='DwnOH'><style id='DwnOH'><dir id='DwnOH'><q id='DwnOH'></q></dir></style></legend>
                • <small id='DwnOH'></small><noframes id='DwnOH'>

                  本文介绍了固定装置的 Django 自然键给出反序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在 SO 上看到了一些与此类似的问题,但似乎没有一个能回答我的特定问题.我是 Django 新手,并按照 此页面上的说明指导自己 允许自己使用自然键来加载固定装置.不过,我收到了反序列化错误,因为 Django 想要一个整数作为外键,并且似乎无法将我的自然键映射到说明中所述的整数主键.具体来说,我的相关模型代码是:

                  I've seen a few similar questions to this on SO, but none seem to answer my particular problem. I'm new to Django and was guiding myself by the instructions at this page to allow myself to use natural keys to load fixtures. Nevertheless, I'm getting Deserialization errors because Django wants an integer for a foreign key and can't seem to map my natural key to an integer primary key as is noted in the instructions. Specifically, my relevant models code is:

                  class GraphTypeManager(models.Manager):
                      def get_by_natural_key(self, type):
                          return self.get(type=type)
                  class GraphType(models.Model):
                      type = models.CharField(max_length=100, unique=True)
                  
                  class GraphManager(models.Manager):
                      def get_by_natural_key(self, name):
                          return self.get(name=name)
                  class Graph(models.Model):
                      name = models.CharField(max_length=200, unique=True)
                      type = models.ForeignKey(GraphType)
                  
                  class LineManager(models.Manager):
                      def get_by_natural_key(self, name):
                          return self.get(name=name)
                  class Line(models.Model):
                      name = models.CharField(max_length=200, unique=True)
                  
                  class GraphToLineManager(models.Manager):
                      def get_by_natural_key(self, line, graph):
                          return self.get(line=line,graph=graph)
                  class GraphToLine(models.Model):
                      line = models.ForeignKey(Line)
                      graph = models.ForeignKey(Graph)
                      class Meta:
                          unique_together = (('line', "graph"),)
                  

                  我的 YAML 夹具是:

                  And my YAML fixture is:

                  - model: graphs_container.GraphType
                    pk: null
                    fields:
                      type: TimeSeries
                  - model: graphs_container.Graph
                    pk: null
                    fields:
                      name: LikesOverTime
                      type: [TimeSeries]
                  - model: graphs_container.Graph
                    pk: null
                    fields:
                      name: UsersOverTime
                      type: [TimeSeries]
                  - model: graphs_container.Line
                    pk: null
                    fields:
                      name: NumUsers
                  - model: graphs_container.Line
                    pk: null
                    fields:
                      name: NumLikes
                  

                  但是在尝试运行 python manage.py loaddata sample_data.yaml 时,我收到以下错误:

                  But when trying to run python manage.py loaddata sample_data.yaml, I get the following error:

                  DeserializationError: [u"'['TimeSeries']' value must be an integer."]
                  

                  我做错了什么?

                  推荐答案

                  你需要

                  • 在模型中定义 natural_key 方法
                  • 拥有一个具有 get_by_natural_key 方法的经理
                  • 实际附加管理器(objects=GraphManager())
                  • define natural_key method in your models
                  • have a manager with get_by_natural_key method
                  • actually attach the managers (objects=GraphManager())

                  在玩过你的代码之后,我让它工作了:

                  After playing with your code, I made it work:

                  class GraphTypeManager(models.Manager):
                      def get_by_natural_key(self, type):
                          return self.get(type=type)
                  
                  class GraphType(models.Model):
                      type = models.CharField(max_length=100, unique=True)
                      objects = GraphTypeManager()
                  
                      def natural_key(self):
                          return (self.type,)  # must return a tuple
                  
                  class GraphManager(models.Manager):
                      def get_by_natural_key(self, name):
                          return self.get(name=name)
                  
                  class Graph(models.Model):
                      name = models.CharField(max_length=200, unique=True)
                      type = models.ForeignKey(GraphType)
                      objects = GraphManager()
                  

                  转储数据:

                  $ bin/django dumpdata index --indent=4 --natural > project/apps/fixtures_dev/initial_data.json
                  [
                      {
                          "pk": 1,
                          "model": "index.graphtype",
                          "fields": {
                              "type": "asotuh"
                          }
                      },
                      {
                          "pk": 1,
                          "model": "index.graph",
                          "fields": {
                              "type": [
                                  "asotuh"
                              ],
                              "name": "saoneuht"
                          }
                      }
                  ]
                  
                  bin/django loaddata project/apps/fixtures_dev/initial_data.json 
                  Installed 2 object(s) from 1 fixture(s)
                  

                  这篇关于固定装置的 Django 自然键给出反序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何为 ruamel.yaml 创建自定义 yaml 映射转储程序 下一篇:使用 python 将十六进制转储到 YAML

                  相关文章

                  最新文章

                    • <bdo id='aATql'></bdo><ul id='aATql'></ul>

                      <small id='aATql'></small><noframes id='aATql'>

                    1. <i id='aATql'><tr id='aATql'><dt id='aATql'><q id='aATql'><span id='aATql'><b id='aATql'><form id='aATql'><ins id='aATql'></ins><ul id='aATql'></ul><sub id='aATql'></sub></form><legend id='aATql'></legend><bdo id='aATql'><pre id='aATql'><center id='aATql'></center></pre></bdo></b><th id='aATql'></th></span></q></dt></tr></i><div id='aATql'><tfoot id='aATql'></tfoot><dl id='aATql'><fieldset id='aATql'></fieldset></dl></div>
                      <tfoot id='aATql'></tfoot>
                    2. <legend id='aATql'><style id='aATql'><dir id='aATql'><q id='aATql'></q></dir></style></legend>