Login

adding fields to User model

Author:
diverman
Posted:
August 2, 2010
Language:
Python
Version:
Not specified
Score:
1 (after 7 ratings)

This code adds new field to Django user model. It must be executed early as much as possible, so put this code to init.py of some application.

1
2
3
4
5
from django.db import models
from django.contrib.auth.models import User

some_field = models.CharField(max_length=32)
some_field.contribute_to_class(User, 'some_field')

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

sberlotto (on August 3, 2010):

How to Sync this modified model with my database ?

#

diverman (on August 3, 2010):

sberlotto:

manage.py sqlall and manage.py syncdb creates new column properly.

What is your problem? If your column doesn't exist aleready, write an ALTER TABLE command.

I don't understand the negative score, since this snippet is working well.

#

udfalkso (on August 17, 2010):

I'm trying this out. So far so good...

#

dnuske (on November 4, 2010):

Hi, while this worked fine for adding a new field, I would like to go forward and know how to make this new field appear in the admin section.

If somebody give me an approach I'll be thankful! =)

#

NtechRajan (on December 1, 2014):

how to retrive this added fields list from model? when i retrive fields using user._meta.get_all_field_names() method i only get default fields, i don't get newly added fields...

#

Please login first before commenting.