【问题标题】:Django Natural Key for Fixtures Give Deserialization Error固定装置的 Django 自然键给出反序列化错误
【发布时间】:2012-09-16 15:29:42
【问题描述】:

我在 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."]

我做错了什么?

【问题讨论】:

  • 我会尝试使用 json 格式并再次转储数据。

标签: python django yaml fixtures natural-key


【解决方案1】:

你需要

  • 在模型中定义 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)

【讨论】:

  • 您需要显示回溯和导致错误的数据,或者重新创建模型和夹具。我看不出有什么问题。我展示的内容对我来说非常有效。
  • 哦,你是对的,我实际上没有,虽然我认为它已加载。重新checkend,发现哪里不对了:manager 必须用一条特殊的线附加到模型上,比如:objects = GraphTypeManager()
猜你喜欢
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
相关资源
最近更新 更多