当我执行python loaddata lecteurs
时,我没有错误和正常输出:Installed 2 object(s) from 2 fixture(s)
。
问题是我输入Django的shell并尝试Lecteurs.objects.all()
这会给我一个空数组。
这就是Lecteur(模特)的样子:
class Lecteur(User):
bibliotheque = models.ManyToManyField('bibliotheque.Livre')
localisation = models.ForeignKey(Localisation)
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
其他灯具装载没有问题。 希望你能提供帮助,我已经足够明确了。
答案 0 :(得分:1)
您的yaml
可能被错误地创建或遗漏了其他User
个详细信息。
好的,我刚刚做了一个示例执行夹具并为我工作。
所以我最初做的是通过代码创建yaml
的序列化,意思是:
跟随:
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.
在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.
然后通过yaml
创建了serializer
转储,并将其添加到文件try.yaml
from django.core import serializers
data = serializers.serialize("yaml", SomeModel.objects.all())
with open('try.yaml', 'w') as f:
f.write(data)
然后是至关重要的部分,我的yaml看起来如何?
像这样:
- 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
虽然我得到了Installed 3 object(s) from 3 fixture(s)
而Lecteur.objects.all()
没有给出任何结果,但这有点混乱。
我注意到,由于存在User
模型继承,yaml
不完整,因为当获取数据时,django无法找到要填充的用户数据。
所以,我做了类似的事情来获取database
:
python manage.py dumpdata --format "yaml" > try.yaml
当我通过将yaml
加载为
python manage.py loaddata try.yaml
我将数据恢复到数据库中。这是因为User
表数据存在于yaml
中,因此django现在可以正确填充数据库。
这是我的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
理想情况下,django应该抱怨,但那可能是一个bug。不确定实施。
特别注意以下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
该代码是一个标识用户在创建自定义类之前必须添加的部分。