简体   繁体   English

Django不加载灯具

[英]Django doesn't load fixtures

When I execute python loaddata lecteurs I have no error and this normal output : Installed 2 object(s) from 2 fixture(s) . 当我执行python loaddata lecteurs我没有错误,并且没有这个正常输出: Installed 2 object(s) from 2 fixture(s) The problem is that wehn I enter in Django's shell and try to Lecteurs.objects.all() this return me an empty array. 问题是我们进入Django的外壳并尝试输入Lecteurs.objects.all()这将返回一个空数组。

This is how Lecteur (model) looks like : 这就是Lecteur(模型)的样子:

class Lecteur(User):
    bibliotheque = models.ManyToManyField('bibliotheque.Livre')
    localisation = models.ForeignKey(Localisation)

And the yaml file called lecteurs.yaml : yaml文件名为lecteurs.yaml:

- model: lecteurs.lecteur
  pk: 1
  fields:
    username: Leila
    bibliotheque: [1, 2]
    localisation: 1
- model: lecteurs.lecteur
  pk: 2
  fields:
    username: Saumon
    bibliotheque: [2, 3]
    localisation: 1
- model: lecteurs.lecteur
  pk: 3
  fields:
    username: Giraffon
    bibliotheque: [1, 4]
    localisation: 1
- model: lecteurs.lecteur
  pk: 4
  fields:
    username: Fany
    bibliotheque: [2, 3]
    localisation: 2

The others fixtures load with no problem. 其他灯具加载没有问题。 Hope you can help and I was enough explicit. 希望您能提供帮助,我很明确。

TL;DR TL; DR

Your yaml might be wrongly created or is missing other User details. 您的yaml可能是错误创建的,或者缺少其他User详细信息。

Explanation 说明

Okay, So I just did a sample executing fixture and works for me. 好的,所以我只是做了一个示例执行夹具并为我工作。

So what I initially did is create a serialize of yaml via code, meaning: 所以我最初要做的是通过代码创建yaml的序列化,这意味着:

Follow along: 跟着:

1. Created a sample model as follows: 1.创建一个示例模型,如下所示:

from django.db import models
from django.contrib.auth.models import User

class Lecteur(User):
    bibliotheque = models.CharField(max_length=100)
    localisation = models.IntegerField()

2. In Django Shell I did the following: 2.在Django Shell中,我执行了以下操作:

python manage.py shell
from tester.models import Lecteur  #tester is my app
Lecteur.objects.create(username="User1", bibliotheque="Hey", localisation=12).save()
Lecteur.objects.create(username="User2", bibliotheque="Hey", localisation=12).save()
Lecteur.objects.create(username="User3", bibliotheque="Hey", localisation=12).save()

3. Then created a yaml dump via serializer and added to a file try.yaml 3.然后通过serializer创建一个yaml转储,并将其添加到文件try.yaml

from django.core import serializers
data = serializers.serialize("yaml", SomeModel.objects.all())
with open('try.yaml', 'w') as f:
    f.write(data)

Then comes the vital part, how does my yaml look? 然后是至关重要的部分,我的Yaml看起来如何?

Like this: 像这样:

- fields:
    bibliotheque: Hey
    groups: []
    localisation: 12
    user_permissions: []
  model: tester.lecteur
  pk: 1
- fields:
    bibliotheque: Hey
    groups: []
    localisation: 12
    user_permissions: []
  model: tester.lecteur
  pk: 2
- fields:
    bibliotheque: Hey
    groups: []
    localisation: 12
    user_permissions: []
  model: tester.lecteur
  pk: 3

Here comes a small confusion, even I got Installed 3 object(s) from 3 fixture(s) and Lecteur.objects.all() gave nothing. 这带来了一个小混乱,即使我Installed 3 object(s) from 3 fixture(s)Lecteur.objects.all()也什么也没给。

Now comes the important stuff: 现在出现了重要的东西:

I noticed, that since there is a User model inheritance, the yaml was incomplete, since django cannot find which user data to fill when the data is obtained. 我注意到,由于存在User模型继承,因此yaml不完整,因为django在获取数据时找不到要填充的用户数据。

So, I did something like this to get the complete data from the database : 因此,我做了这样的事情来从database获取完整的数据:

python manage.py dumpdata --format "yaml" > try.yaml

When I recreated the database by loading the yaml as: 当我通过加载yaml来重新创建数据库时:

python manage.py loaddata try.yaml

I get the data back in the database. 我将数据恢复到数据库中。 This is because the User table data is present in the yaml , so django now can populate the database correctly. 这是因为yaml存在User表数据,因此django现在可以正确填充数据库了。

Here is my same try.yaml file: 这是我相同的try.yaml文件:

- fields: {app_label: contenttypes, model: contenttype, name: content type}
  model: contenttypes.contenttype
  pk: 4
- fields: {app_label: auth, model: group, name: group}
  model: contenttypes.contenttype
  pk: 2
- fields: {app_label: tester, model: lecteur, name: lecteur}
  model: contenttypes.contenttype
  pk: 7
- fields: {app_label: auth, model: permission, name: permission}
  model: contenttypes.contenttype
  pk: 1
- fields: {app_label: sessions, model: session, name: session}
  model: contenttypes.contenttype
  pk: 5
- fields: {app_label: sites, model: site, name: site}
  model: contenttypes.contenttype
  pk: 6
- fields: {app_label: auth, model: user, name: user}
  model: contenttypes.contenttype
  pk: 3
- fields: {domain: example.com, name: example.com}
  model: sites.site
  pk: 1
- fields: {codename: add_group, content_type: 2, name: Can add group}
  model: auth.permission
  pk: 4
- fields: {codename: change_group, content_type: 2, name: Can change group}
  model: auth.permission
  pk: 5
- fields: {codename: delete_group, content_type: 2, name: Can delete group}
  model: auth.permission
  pk: 6
- fields: {codename: add_permission, content_type: 1, name: Can add permission}
  model: auth.permission
  pk: 1
- fields: {codename: change_permission, content_type: 1, name: Can change permission}
  model: auth.permission
  pk: 2
- fields: {codename: delete_permission, content_type: 1, name: Can delete permission}
  model: auth.permission
  pk: 3
- fields: {codename: add_user, content_type: 3, name: Can add user}
  model: auth.permission
  pk: 7
- fields: {codename: change_user, content_type: 3, name: Can change user}
  model: auth.permission
  pk: 8
- fields: {codename: delete_user, content_type: 3, name: Can delete user}
  model: auth.permission
  pk: 9
- fields: {codename: add_contenttype, content_type: 4, name: Can add content type}
  model: auth.permission
  pk: 10
- fields: {codename: change_contenttype, content_type: 4, name: Can change content
      type}
  model: auth.permission
  pk: 11
- fields: {codename: delete_contenttype, content_type: 4, name: Can delete content
      type}
  model: auth.permission
  pk: 12
- fields: {codename: add_session, content_type: 5, name: Can add session}
  model: auth.permission
  pk: 13
- fields: {codename: change_session, content_type: 5, name: Can change session}
  model: auth.permission
  pk: 14
- fields: {codename: delete_session, content_type: 5, name: Can delete session}
  model: auth.permission
  pk: 15
- fields: {codename: add_site, content_type: 6, name: Can add site}
  model: auth.permission
  pk: 16
- fields: {codename: change_site, content_type: 6, name: Can change site}
  model: auth.permission
  pk: 17
- fields: {codename: delete_site, content_type: 6, name: Can delete site}
  model: auth.permission
  pk: 18
- fields: {codename: add_lecteur, content_type: 7, name: Can add lecteur}
  model: auth.permission
  pk: 19
- fields: {codename: change_lecteur, content_type: 7, name: Can change lecteur}
  model: auth.permission
  pk: 20
- fields: {codename: delete_lecteur, content_type: 7, name: Can delete lecteur}
  model: auth.permission
  pk: 21
- fields:
    date_joined: 2014-10-25 07:41:32.935000+00:00
    email: ''
    first_name: ''
    groups: []
    is_active: true
    is_staff: false
    is_superuser: false
    last_login: 2014-10-25 07:41:32.935000+00:00
    last_name: ''
    password: ''
    user_permissions: []
    username: User1
  model: auth.user
  pk: 1
- fields:
    date_joined: 2014-10-25 07:41:41.199000+00:00
    email: ''
    first_name: ''
    groups: []
    is_active: true
    is_staff: false
    is_superuser: false
    last_login: 2014-10-25 07:41:41.199000+00:00
    last_name: ''
    password: ''
    user_permissions: []
    username: User2
  model: auth.user
  pk: 2
- fields:
    date_joined: 2014-10-25 07:41:48.713000+00:00
    email: ''
    first_name: ''
    groups: []
    is_active: true
    is_staff: false
    is_superuser: false
    last_login: 2014-10-25 07:41:48.713000+00:00
    last_name: ''
    password: ''
    user_permissions: []
    username: User3
  model: auth.user
  pk: 3
- fields:
    bibliotheque: Hey
    groups: []
    localisation: 12
    user_permissions: []
  model: tester.lecteur
  pk: 1
- fields:
    bibliotheque: Hey
    groups: []
    localisation: 12
    user_permissions: []
  model: tester.lecteur
  pk: 2
- fields:
    bibliotheque: Hey
    groups: []
    localisation: 12
    user_permissions: []
  model: tester.lecteur
  pk: 3

Ideally django should have complained but then might be a bug. 理想情况下,django应该会抱怨,但是可能是一个错误。 Not sure about the implementation. 不确定执行情况。

Notice specifically the following yaml code: 请特别注意以下yaml代码:

- fields:
    date_joined: 2014-10-25 07:41:32.935000+00:00
    email: ''
    first_name: ''
    groups: []
    is_active: true
    is_staff: false
    is_superuser: false
    last_login: 2014-10-25 07:41:32.935000+00:00
    last_name: ''
    password: ''
    user_permissions: []
    username: User1
  model: auth.user
  pk: 1

That code is a piece that identifies the user you would have to add before the custom class is created. 该代码是一段代码,用于标识在创建自定义类之前必须添加的用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM