Derek
Derek

Reputation: 12388

Where to you monkey patch the Django user model?

I want to monkey patch the user model from Django.

My code:

from django.db import models

from django.contrib.auth.models import User
User.add_to_class('secret_question', models.CharField(max_length="100"))
User.add_to_class('answer', models.CharField(max_length="100"))
User.add_to_class('DOB', models.DateField())

Where do I place this code so that python manage.py syncdb will create the correct table?

I tried the main directory models.py, I tried an app's directory's models.py (these two didn't produce the correct table), and I tried placing it in the settings.py of the project (error, couldn't run).

Upvotes: 3

Views: 2749

Answers (2)

miku
miku

Reputation: 188114

Please take a look at Storing additional information about users section of the authentication documentation. It suggests a cleaner way to add additional information to a User object.

If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose.

Upvotes: 7

Aldarund
Aldarund

Reputation: 17621

If you really want to monkey patch user model, there already exists an app for this. Django-primate

A modular django user.

This Django application monkey patches django in order to have a custom User model that plugs into the django.contrib.auth application.

Upvotes: 3

Related Questions