简体   繁体   English

如何在 Django 中添加另一个模型的模型对象时自动创建一个模型的模型对象

[英]How to create model objects of one model automatically when a model object of another has been added in django

Say for example I have a Model called Player.比如说我有一个名为 Player 的模型。 I want Model objects of Player created when new Users are added in django (from django.contrib.auth.models.User ) such that each User object in User model has its own object in Player model.我希望在 django 中添加新用户(来自django.contrib.auth.models.User )时创建 Player 的 Model 对象,以便 User 模型中的每个 User 对象在 Player 模型中都有自己的对象。 I know that I would technically have to use models.ForeignKey() to create a field in Player model and relate it to the User model and that I don't have to worry about deletion of users if I use the on_delete=models.CASCADE parameter in models.ForeignKey() but how do I automatically create these objects with like default values and such.我知道我在技术上必须使用models.ForeignKey()在 Player 模型中创建一个字段并将其与 User 模型相关联,如果我使用on_delete=models.CASCADE ,我不必担心删除用户models.ForeignKey()参数但是我如何使用默认值等自动创建这些对象。 Initially this was my code to do so:最初这是我的代码:

for name in User.objects.all().values_list('username', flat=True):
if name not in Player.objects.all().values_list('player_name', flat=True):
    new_player = Player(player_name=name, current_level_no=None, no_of_moves=None)
    new_player.save()
else:
    pass

But this would give me a DatabaseIntegrityError that "Models aren't loaded yet".但这会给我一个 DatabaseIntegrityError “模型尚未加载”。 So I am confused about what to do or how to go forward.所以我对做什么或如何前进感到困惑。

As always, I greatly appreciate all answers!一如既往,我非常感谢所有的答案!

Check out Django signals , specifically the post_save signal .查看Django 信号,特别是post_save 信号 Essentially, Django lets you define a function that will be run whenever your User model is saved.本质上,Django 允许您定义一个函数,该函数将在您的 User 模型被保存时运行。 You'd use it something like this:你会像这样使用它:

from django.db.models.signals import post_save

def create_player(sender, instance, created, **kwargs):
    if created:
        Player.objects.create(user=instance)  # instance is the User object

post_save.connect(create_player, sender=User)

Note that if you already have User objects in your database, this won't create Player objects for them;请注意,如果您的数据库中已经有 User 对象,则不会为它们创建 Player 对象; you'll want to do that by running a loop similar to the one you posted in the Django shell (using python manage.py shell ).您需要通过运行一个类似于您在 Django shell 中发布的循环(使用python manage.py shell )来做到这一点。 The signal will run on every subsequent save of a user model.该信号将在用户模型的每次后续保存中运行。

暂无
暂无

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

相关问题 如何在创建另一个Model对象的同时自动创建Django Model对象 - How To Automatically Create Objects Of Django Model While Creation Another Model Objects 模型的Django表单创建另一个模型的对象 - Django form for a model create object of another model 如何在 Django 中创建一个模型的对象,同时创建另一个不同的模型? - How to create in Django an object of a model while creating another of a different model? 如何检测何时添加/删除Django模型的对象? - How can I detect when objects of a Django model are added/deleted? Django在创建另一个对象时创建并保存许多模型实例 - Django Create and Save Many instances of model when another object are created 如何在Django中以多对一关系将一个模型对象添加到另一模型的ModelForm模板中? - How do I add one model objects to another model’s ModelForm template with many to one relationship in Django? 当嵌套序列化程序中有另一个模型(manytomany)时,我如何在 django 模型上发布,我想同时创建两者 - How do I post on a django model when it has another model (manytomany ) inside nested serializers, I want at the same time to create both 如何将一个对象添加到具有 ManyToManyField 的 Django 模型中? - How do you add one object to a Django Model that has a ManyToManyField? Django:引用在另一个模型中定义的一个多字段 - Django: Referencing a manytomany field that has been defined in another model 我们如何从 model 中选择对象以在另一个 model 中创建副本,在 django 中添加额外参数? - How do we pick objects from a model to create copies in another model adding extra parameters in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM