0

I am in trouble in populating a table, at first see my models.

from django.contrib.auth.models import User


class Group(models.Model):
    members = models.ManyToManyField(
        User,
        through='PersonGroup',
        related_name='person_of_the_group'
    )


class PersonGroup(models.Model):
    group = models.ForeignKey(
        Group,
        on_delete=models.CASCADE,
        related_name='group_person_of_group'
    )
    person = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name='group_person_of_group'
    )

I want when I create a group, the PersonGroup should populate automatically, I tried to achieve it in the signal but failed to do it, coz, I couldn't access request.user in signal

@receiver(post_save, sender=Group)
def create_person(sender, instance, created, **kwargs):
    if created:
        PersonGroup.objects.create(
            #
        )

Can anyone help to access to request in the signal? I need to solve this problem in signal at any means

  • 1
    Ain't gonna be possible. You're working with signals which are mostly model functionality, which won't know about `request` at any means. Not sure why you wanna do this in a signal. Can be easily done in a View. – Alex Oct 10 '19 at 18:28
  • How to do it in view? –  Oct 10 '19 at 19:33
  • Create Group -> create PersonGroup with the group created + request.user :) – Alex Oct 10 '19 at 22:23
  • This is not best way... –  Oct 11 '19 at 03:32

0 Answers0