开发者

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).


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜