Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django CBV内部使用CreateView创建对象后获取对象_Python_Django_Mixins_Django Class Based Views_Django Generic Relations - Fatal编程技术网

Python 如何在django CBV内部使用CreateView创建对象后获取对象

Python 如何在django CBV内部使用CreateView创建对象后获取对象,python,django,mixins,django-class-based-views,django-generic-relations,Python,Django,Mixins,Django Class Based Views,Django Generic Relations,我正在尝试创建一个跟踪我的用户所有活动的通知系统。为了实现这一点,我创建了两个模型,贡献模型和通知模型 class Contribution(models.Model): slug = models.SlugField(unique=True, blank=True, null=True) user = models.ForeignKey(User, on_delete=models.PROTECT) amount

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

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)
我想在用户每次在贡献表中创建对象时创建一个通知对象,但在从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\u有效的
方法中创建的,因此在调用该方法之前,您无法访问它。而是先调用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

该对象是在super
form\u 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 save信号:

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=???,)

一种优雅的方法是使用post save信号:

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=???,)