如何在使用 Django CBV 中的 CreateView 创建对象后获取对象

How to get an object after been created using CreateView inside django CBV

我正在尝试创建一个通知系统来跟踪我的用户的所有活动。为此,我创建了两个模型,贡献模型和通知模型

class Contribution(models.Model):
    slug            =   models.SlugField(unique=True, blank=True, null=True)
    user            =   models.ForeignKey(User, on_delete=models.PROTECT)
    amount          =   models.DecimalField(default=0.00, max_digits=6, decimal_places=2)
    zanaco_id       =   models.CharField(max_length=20, blank=True, unique=True, null=True)

class Notification(models.Model):
    slug        =   models.SlugField(unique=True, blank=True)
    content_type    =   models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id       =   models.PositiveIntegerField()
    content_object  =   GenericForeignKey('content_type', 'object_id')
    message         =   models.TextField(null=True)

每当用户在 Contribution table 中创建对象时,我都想创建一个 Notification 对象,但是在从 CreateView

中获取创建的对象时遇到了一些困难
class ContributionAdd(CreateView):
    model           =   Contribution
    fields          = ['user', 'amount', 'zanaco_id']
    template_name   =   'contribution_add.html'


    def form_valid(self, form, *args, **kwargs):
        activity_ct = ContentType.objects.get_for_model("????")
        Notification.objects.create(content_type=activity_ct, object_id="?????",content_object=???,)
        return super().form_valid(form)

我怎样才能完成上面的任务? 他们是使用 mixin 来做到这一点的方法吗?

该对象是在 super form_valid 方法中创建的,因此在该方法被调用之前您无法访问它。而是先调用super方法,用self.object引用创建的对象:

class ContributionAdd(CreateView):
    model           =   Contribution
    fields          = ['user', 'amount', 'zanaco_id']
    template_name   =   'contribution_add.html'


    def form_valid(self, form):
        response = super().form_valid(form) # call super first
        Notification.objects.create(content_object=self.object) # Why even pass the other values just pass `content_object` only
        return response

一个优雅的方法是使用 post 保存信号:

from django.dispatch import receiver
from django.db.models.signals import post_save

@receiver(post_save, sender=Contribution)
def createNotification(sender, instance, created, **kwargs):
    if created:
        Notification.objects.create(content_type=activity_ct, object_id="?????",content_object=???,)