0

I have a model called Team, with a ForeignKey relationship to Agent (my user model). When an Agent who is a team_leader deactivates the team, all the Agents with team_member set to the current team will have their team_member attribute removed and set to NULL/empty.

I read this and this, I know I'm either supposed to do something with ._meta.get_fields() or with the Collector class. I'm experimenting with the get_fields() but haven't managed to get it working.

models.py

class Agent(AbstractUser):
    team_member = models.ForeignKey('AgentTeam', on_delete=models.CASCADE, null=True, blank=True, related_name='team_member')
    team_leader = models.OneToOneField('AgentTeam', on_delete=models.CASCADE, null=True, blank=True, related_name='team_leader')

class AgentTeam(models.Model):
    team_is_active = models.BooleanField(default=False)

views.py

def deactivate_team(request):
    request.user.AgentTeam.team_is_active = False  # request.user in this case is the team_leader

    # I need to set all the Agent's with team_member set to the request.user's team to NULL.  I have experimented with the code below and gotten only errors, mostly related to ReverseToOne like ''ReverseManyToOneDescriptor' object is not iterable'
    agents = [
        f for f in AgentTeam._meta.get_fields()
        if (f.one_to_many)
        and f.auto_created and not f.concrete
    ]
    for agent in agents:
        objects = getattr(AgentTeam, agent.name).all()
        for object in objects:
            object.team_member = None

Please provide some comments and pointers on my views code and any errors I made. Thank you.

Valachio
  • 1,025
  • 2
  • 18
  • 40

1 Answers1

1

If I understood correctly, I don't see a reason why you need to iterate through the model fields.

You might be able to get away without having to bother with any fields of the model. You can just update the team_member status of all agents when the AgentTeam value is nullified. This could be done in the view, or in the model save() method. For example:

class Agent(AbstractUser):
    team_member = models.ForeignKey('AgentTeam', on_delete=models.CASCADE, null=True, blank=True, related_name='team_member')
    team_leader = models.OneToOneField('AgentTeam', on_delete=models.CASCADE, null=True, blank=True, related_name='team_leader')

class AgentTeam(models.Model):
    team_is_active = models.BooleanField(default=False)

    def save(self, *args, **kwargs):
        super(AgentTeam, self).save(*args, **kwargs)
        if not self.team_is_active:
            Agent.objects.filter(
                team_member=self
            ).update(team_member=None)

And then within the view:

def deactivate_team(request):
    team = request.user.AgentTeam
    team.team_is_active = False
    team.save()
Robert Townley
  • 3,414
  • 3
  • 28
  • 54
  • Perfect. That worked. I had to use `request.user.team_leader` instead of `request.user.AgentTeam`. I got a ['Agent' object has no attribute 'AgentTeam'] error when I used the latter. Thank you! – Valachio Nov 09 '17 at 17:25