How to send variables from view.py to a signal in Django

i want django to create a ServerModeratorPermission object just after the user has been set as the moderator of the server

views.py:

class ModeratorSettingsView(LoginRequiredMixin, View):
form_class = AddModeratorForm

...

def post(self, request, server_tag):
    form = self.form_class(request.POST)

    user = User.objects.get(pk=request.POST.get('user'))
    server = Server.objects.get(tag=server_tag)
    moderators = server.moderator_of.exclude(user=server.creator)
    ServerModerator.objects.get_or_create(user=user)
    moderator = ServerModerator.objects.get(user=user)
    moderator.server.add(server)
    return render(request, 'servers/moderator-settings.html', {'moderators':moderators, 'server':server, 'form':form})

signals.py:

@receiver(post_save, sender=ServerModerator)
    def give_default_permission(instance, *args, **kwargs):
        # i need server_tag for this query
        # server = Server.objects.get(tag=server_tag)
        moderator = ServerModerator.objects.get(pk=instance.id)
        ServerModeratorPermission.objects.create(moderator=moderator, server=server)

is there anyway to send server_tag that being used in the view class to the signal?

If server_tag is a field, try to server = Server.objects.get(tag=instance.server_tag)

Back to Top