每次在管理面板中创建新组时,我都需要创建模型的实例。
我阅读了一些有关信号的信息,但我根本无法理解。
非常感谢
答案 0 :(得分:3)
models.py
与您的模型:
from django.db import models
from django.contrib.auth.models import Group
from django.db.models.signals import post_save
class YourModel(models.Model):
name = models.CharField('name', max_length=50)
group = models.ForeignKey(Group)
# ...
@classmethod
def create_after_group(cls, sender, instance, created, **kwargs):
if created:
group_created = instance
m = cls(name="Some name", group=group_created)
m.save()
post_save.connect(YourModel.create_after_group, sender=Group)