There is a task: provided that a tick is pressed on the page (and it is pressed by default), you need to perform the function of interest after saving the record in the database. The function must be performed after saving the record, because it takes the QuerySet with a new record as input and there is no possibility to influence the behavior of the function. Actually the question is how to transfer the parameter of this tick to the part of the program that is executed after saving the record.
It was decided to sit down on the post_save signal, since it gives the opportunity to perform the desired function even when creating a new record in the database and changing the old one.
Actually some code:

class AssetCreateView(CreateModelView): @receiver(post_save, sender=models.Asset) def asset_post_save(sender, **kwargs): print('post save callback') if kwargs.has_key('instance'): qs = models.Asset.objects.filter(as_macro=kwargs['instance'].as_macro) r = request.HttpRequest() actions_bulk.fetch_asset_routes_and_merge(request=r, queryset=qs) def get_initial(self): initial = super(AssetCreateView, self).get_initial() if self.request.POST.has_key("get_as") and self.request.POST["get_as"] == "on": print('\n\nhere!\n\n') return initial 

Where:

  • get_as is a parameter depending on which the fetch_asset_routes_and_merge function should not be executed fetch_asset_routes_and_merge
  • asset_post_save() - function executed at the moment after the post is saved to the database

What I would like to achieve architecturally:

 @receiver(post_save, sender=models.Asset) def asset_post_save(sender, **kwargs): if get_as == 'on': qs = models.Asset.objects.filter(as_macro='some_var') r = request.HttpRequest() actions_bulk.fetch_asset_routes_and_merge(request=r, queryset=qs) 

Applicable stack: Django 1.11, django-material

    2 answers 2

    You are inheriting from CreateModelView, I would venture to guess that it is inherited from CreateModelMixin, which is inherited in turn from CreateModelMixin. Saving in the database occurs at the level of the serializer, which in CreateModelMixin is called in the method:

     def perform_create(self, serializer): serializer.save() 

    Accordingly, you need to override this method:

     def perform_create(self, serializer): serializer.save() if get_as: asset_post_save() 
    • Inheritance really comes from CreateModelView, but the class itself is inherited from others other than what you specified: CreateModelView (MessageUserMixin, ModelViewMixin, generic.CreateView). Actually, your method unfortunately did not work. - anderson
    • Then try redefining the post method that CreateView has - virvaldium am
    • one
      You're right! It worked! Many thanks! - anderson

    As correctly suggested in the comments, it was necessary to override the method of the inherited class. The class took the form:

     class AssetCreateView(CreateModelView): def get_initial(self): initial = super(AssetCreateView, self).get_initial() if self.request.GET.has_key("as-macro"): initial['as_macro'] = self.request.GET["as-macro"] return initial def post(self, request, *args, **kwargs): res = super(AssetCreateView, self).post(self, request) if self.request.POST.get('get_as') == 'on': qs = models.Asset.objects.filter(as_macro=self.request.POST.get('as_macro')) from django.http import request r = request.HttpRequest() actions_bulk.fetch_asset_routes_and_merge(request=r, queryset=qs) return res