Gabriel
Gabriel

Reputation: 332

How to change a field that automatically affect other records in Django

How to change a field in a specific record that automatically affect the same field on other records?

Consider the following model:

class AccountUser(models.Model):
    ADMIN='A'
    USER='U'
    USER_PROFILES = (
        (ADMIN, 'Admin'),
        (USER, 'User'),
    )
    account = models.ForeignKey(Account, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    profile = models.CharField(max_length=1,choices=USER_PROFILES,default=USER)
    current_account = models.BooleanField(default=True)

    def __str__(self):
    return format('{} ({})'.format(self.account.name, self.user.username))

This is a many-to-many relation between users and accounts, so users can be in many accounts but every user can have one and only one current account. Then, everytime a new record is created or updated with current_account = True, any other records from the same user should be updated with current_account = False.

I've saw a similar question in Actions triggered by field change in Django, but it is for changes in the same record.

Upvotes: 0

Views: 214

Answers (1)

Manuel Fedele
Manuel Fedele

Reputation: 1709

I think that you can achieve this with post_save signals. I hope i understood the logic you mean, but you can simply refactor the post_save staticmethod to achieve your objective.

from django.db.models.signals import post_save

class AccountUser(models.Model):
    ADMIN='A'
    USER='U'
    USER_PROFILES = (
        (ADMIN, 'Admin'),
        (USER, 'User'),
    )
    account = models.ForeignKey(Account, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    profile = models.CharField(max_length=1,choices=USER_PROFILES,default=USER)
    current_account = models.BooleanField(default=True)

    @staticmethod
    def post_save(sender, instance):
        records = AccountUser.objects.filter(user = instance.user)
        for record in records:
            record.current_account = True
            record.save()

    def __str__(self):
        return format('{} ({})'.format(self.account.name, self.user.username))

 post_save.connect(AccountUser.post_save, sender=AccountUser)

Upvotes: 1

Related Questions