ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 夹具的Django自然键给出反序列化错误

2019-09-01 20:08:55  阅读:231  来源: 互联网

标签:fixtures python django yaml natural-key


我在SO上看到了一些类似的问题,但似乎没有人回答我的特殊问题.我是Django的新手,并在this page的指示下指导自己允许自己使用自然键加载灯具.然而,我得到反序列化错误,因为Django想要一个外键的整数,并且似乎无法将我的自然键映射到整数主键,如说明中所述.具体来说,我的相关模型代码是:

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夹具是:

- 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时,我收到以下错误:

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

我究竟做错了什么?

解决方法:

你需要

>在模型中定义natural_key方法
>让经理使用get_by_natural_key方法
>实际附加管理器(objects = GraphManager())

在使用您的代码后,我让它工作:

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)

标签:fixtures,python,django,yaml,natural-key
来源: https://codeday.me/bug/20190901/1785535.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有