1

I'm trying to override the default User model in Django to add some logic into the save() method. I'm having a hard time trying to figure out out to go about this.

I'm using Django 1.1 if that helps.

I used post_save since i need to add the user into ldap.. I just added this into a models.py

from django.db import models from django.contrib.auth.models import User from django.db.models import signals from django.dispatch import dispatcher def user_post_save(sender, instance, **kwargs): print "got here" models.signals.post_save.connect(user_post_save, sender=User) 
2

2 Answers 2

4

Don't. Instead catch the pre_save signal.

Sign up to request clarification or add additional context in comments.

3 Comments

How? What would he do in the pre_save signal? Can you give an example of how to do it?
@Dominic: The signal handler is passed the model before it's put into the database. You just manipulate it the way you normally would.
@dominic.. i updated my post with a example solution based on post_save signal
2

You'd better use a Proxy model, so to use the same table but overriding behavior.

This is the standard way to extend Django's own models, because they cannot be made abstract.

So declare your model as:

from django.contrib.auth.models import User class CustomUser(User): class Meta: proxy = True def save(self, *args, **kwargs): # do anything you need before saving super(CustomUser, self).save(*args, **kwargs) # do anything you need after saving 

and you are done.

2 Comments

I used proxy models and it breaks up couple of things such as urls in the admin site. Don't think its a good choice.
Right. Now that I know much more django than 7 months ago, I'd go straight to the signal path.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.