79141322

Date: 2024-10-30 13:39:13
Score: 0.5
Natty:
Report link

There are a lot of examples out there, but this is what i do. I created a "Profile" model and then tied it to their User record. This prevents me from having to jack with the User record while also being able to store/track whatever i want and tied to the User account. When a user is created, it will automatically generate a Profile record setting defaults... and grant access to a group automatically. I need to refine how i'm identifying the group better than using the PK, but this works good enough for now. While this may not solve your issue specifically, i think the structure/processes could be easily adapted to accomplish what you're looking to do.

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    discipline_assessed = models.BooleanField(default=False, verbose_name='Assessed?')
    allow_skills = models.BooleanField(default=False, verbose_name='Allow Access To Skills Matrix Tool?')
    def __str__(self):
        return self.user.username
    class Meta:
        ordering = ['user',]
@receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        try:
            if created:
                Profile.objects.create(user=instance)
                instance.groups.add(Group.objects.get(id=1))
                instance.save()
        except Exception as err:
            print('Error creating user profile!')
@receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()
Reasons:
  • Blacklisted phrase (0.5): I need
  • RegEx Blacklisted phrase (1): i want
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Eric