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_asis a parameter depending on which thefetch_asset_routes_and_mergefunction should not be executedfetch_asset_routes_and_mergeasset_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