gpt4 book ai didi

python - 是否可以使用 Django 的固定装置更改现有数据?

转载 作者:行者123 更新时间:2023-11-28 22:02:01 25 4
gpt4 key购买 nike

我在测试中使用 Django 的固定装置。我需要创建 UserProfile fixture。第一个问题是它指向用户条目。为了处理它,我使用自然键。所以我只是说 UserProfile fixture 的用户字段是 User fixture 的实际用户名。

[
{
"pk": 8002,
"model": "auth.user",
"fields": {
"date_joined": "2012-08-28 10:00:00.000000+00:00",
"email": "",
"first_name": "",
"groups": [],
"is_active": true,
"is_staff": false,
"is_superuser": false,
"last_login": "2012-08-28 10:00:00.000000+00:00",
"last_name": "",
"password": "pbkdf2_sha256$10000$ltmEKsdCPjuK$74ZwNUh8rqFAPZ6+Cmi3tc8A94ueeXTplFqOQKlY4hc=",
"user_permissions": [],
"username": "user8002"
}
},
{
"pk": null,
"model": "spam_and_eggs.userprofile",
"fields": {
"user": ["user8002"],
"language": "",
"email_activated": true
}
}
]

不幸的是,它返回一个错误:

IntegrityError: Could not load share4you.UserProfile(pk=None): column user_id is not unique

第二个问题来了。我认为它可能会失败,因为在使用 Django 的信号创建用户时会自动创建 UserProfile,而 fixture 会失败,因为该用户的 UserProfile 已经创建。会是这个原因吗?有什么办法可以解决吗?

感谢任何建议!

编辑#1:

模型和信号:

class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
language = models.CharField(max_length=255, null=True, blank=True)
email_activated = models.BooleanField()

@receiver(post_save, sender=User)
def create_profile(instance, created, **kwargs):
if created:
UserProfile.objects.get_or_create(user=instance)

用户模型是 django.contrib.auth.models.User

最佳答案

Is it possible to alter existing data with Django's fixtures?

当然,如果 fixture 有一个存在于数据库中的 pk,django 将进行更新。

user = models.ForeignKey(User, unique=True)

这很有趣,在那种情况下为什么不用 OneToOneField 呢?

>>> a=User.objects.all()[0]

>>> a.userprofile_set.all()
[<UserProfile: UserProfile object>]

>> a.userprofile_set.create(language='bar')
IntegrityError: column user_id is not unique

IntegrityError: Could not load share4you.UserProfile(pk=None): column user_id is not unique

  • 代替pk=null,设置一个实数
  • 您应该设置 "user": 8002 而不是 "user": ["user8002"]

I think that it may fail because UserProfile is automatically created when user is created using Django's signals and fixture fails because UserProfile for that User is already created.

我无法重现此行为。这些装置可以很好地加载和重新加载您的模型:

[
{
"pk": 1,
"model": "auth.user",
"fields": {
"username": "jpic",
"first_name": "",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2012-06-11T06:44:57.637Z",
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$10000$KzsUTACvZgJU$qvywXdVv/N3s5lifS/gQxSGog36ExGbuj2U+IQ6aUNk=",
"email": "t@tt.tt",
"date_joined": "2012-06-11T06:44:57.637Z"
}
},
{
"pk": 1,
"model": "test_app.userprofile",
"fields": {
"language": "example.com",
"user": 1
}
}
]

此外,这里还有另一种创建 UserProfile 类的可能性,使用 django-annoying :

from annoying.fields import AutoOneToOneField


class UserProfile(models.Model):
user = AutoOneToOneField('auth.User', primary_key=True)
home_page = models.URLField(max_length=255, blank=True)

关于python - 是否可以使用 Django 的固定装置更改现有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12156346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com