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.
- [Answered ]-Django Nginx Gunicorn DEBUG Flag Error
- [Answered ]-Serve a file for download with Django
- [Answered ]-Django HTML for loop not showing corresponding values
- [Answered ]-Generating django models from xml
- [Answered ]-Django tinymce safe template tag
Source:stackexchange.com