[Answered ]-Manager for a user profile to create and delete both user and the profile

1👍

class PersonnelManager(models.Manager):
    def create(self, username, email, phone, address, **kwargs):
        user = User.objects.get_or_create(username=username, email=email)
        return super(PersonnelManager, self).create(user=user, phone=phone, address=address, **kwargs)

class Personnel(models.Model):
    ...
    objects = PersonnelManager()

Deleting should take care of itself through the cascade.

1👍

It’s probably easier to add methods to Personnel.save() and Personnel.delete() to do this work.

https://docs.djangoproject.com/en/1.3/topics/db/models/#overriding-model-methods

The Personnel.save() is called by Django and can create a missing User and Profile.

The “Overriding Delete” sidebar may not be relevant, depending on your application. Bulk deletes are rare and it’s easy to do a post-bulk-delete cleanup. Or do individual deletes instead of a bulk delete.

👤S.Lott

Leave a comment