سؤال

I'm working on a project in Django and I have the following problem:

I have these two classes, Team and Project. When I create one project I want to automatically assign users from the team what was selected when I created the Project to the new project. I override Project's save method and after the project was created assign users to the project(I did after saving because, before gave me an error). I tried of several ways but none of them works. Tried by:

self.user.add(*self.team.users.all())
self.save()

And this doesn't work.

Tried iterating:

for uTeam in self.team.users.all():
    self.users.add(uTeam)

and doesn't work either.

The only way that work for me is this, but only in the Django Shell:

P = Project.objects.get(pk=1)
T = Team.objects.get(pk=1)
P.user.add(*T.user.all())
P.save()

This is the solution that I have below but doesn't work in Django(gives an infinite loop)

class Team(models.Model):
    name = models.CharField(max_length=200,
                            help_text=_('name of the team'))
    user = models.ManyToManyField(settings.AUTH_USER_MODEL,
                                related_name="members_of_team",
                                help_text=_('users of the team'),
                                null=True
                                )
    and more....


class Project(models.Model):
    id = models.AutoField(primary_key=True, null=False)
    name = models.CharField(max_length=200,
                            help_text=_('name of project'),
                            )
    team = models.ForeignKey(Team,`enter code here`
                             on_delete=models.PROTECT,
                             help_text=_('team of project'))
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL,
                                  on_delete=models.PROTECT,
                                  related_name='creator_project',
                                  blank=True,
                                  null=False,
                                  help_text=_('project created by'))
    customer = models.ForeignKey(Customer,
                                on_delete=models.PROTECT,
                                help_text=_('customer'))
    user = models.ManyToManyField(settings.AUTH_USER_MODEL,
                                  related_name='users_team',
                                  blank=True,
                                  null=False,
                                  help_text=_('users of this project'))


   def save(self, *args, **kwargs):

     if self.checkIntegrity():
       super(Project, self).save(*args, **kwargs)
       if self.user.all().count() < self.team.user.all().count():
         T = Team.objects.get(pk=self.team.id)
         P = Project.objects.get(pk=self.id)
         P.user.add(*T.user.all())
         P.save()

Thank you for your help

هل كانت مفيدة؟

المحلول

I got it, I read that m2m fields are filled after save() and post_save() and there is a signal that trigger when a m2m field is changed so I write the following:

@receiver(m2m_changed, sender=Project.user.through)
def m2mChange(sender, **kwargs):
    instance = kwargs['instance']
    T = Team.objects.get(pk=instance.team.id)
    if kwargs['pk_set'] is None:
         instance.user.add(*T.user.all())

And now it works fine. Thank you for all.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top