Take the 2-minute tour ×

I have a Django app that needs an extra field (a manytomany mapping to a Book class) and I did that by extending the AbstractUser.

If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.

class User(AbstractUser):
    entitledBooks = models.ManyToManyField(Book)

I added the model in my settings.py

AUTH_USER_MODEL = 'BookRServer.User'

But how do I create a user (programmatically) ?

user = User.objects.create_user() doesn't work.

share|improve this question
What do you mean by "doesn't work"? Stack trace, please? Also, are you importing User from your models? I think the Django suggested way is to use User = get_user_model() instead. –  Kimvais Dec 17 '13 at 8:20
Argh.. I should've been more articulate. I can "create" a user, but I cannot add a "book" to "entitledBooks". It says "object user has no attribute entitledBooks". I can only assume that it is creating the original User and not my custom User. –  user1349663 Dec 17 '13 at 8:25
add comment

2 Answers

up vote 1 down vote accepted

The create_user manager method only accepts three arguments, username, email (optional), and password (optional).

You can do so:

user = User.objects.create_user('testuser',email='1@gmail.com')
share|improve this answer
I used user.entitledBooks.add, but yeah, I had to create the user object and then add the extra fields to it. So I'll accept this answer. –  user1349663 Dec 18 '13 at 4:51
add comment

At the simple possible, loosely coupled way is to create a One-to-One relationship user profile model containing your many-to-many relationship

class UserProfile(models.Model):
    user = model.OneToOneField(User)
    # your many-to-many field here

# create user
u = User(username="", password="", email="", ...)
u.userprofile(your fields..here)

This is typically same in all django versions. What basically django 1.6 deprecated is to provide a model method get_profile and some relevant settings..

Unless you really want to build User model right from the ground as per requirements, you don't have to really use AUTH_USER_MODEL..

Detaching User Profile from User model also helps in the long run

share|improve this answer
Ah. Thanks for letting me know about AUTH_USER_MODEL. But since AbstractUser is provided, I'd like to know how to use it. –  user1349663 Dec 17 '13 at 8:34
refer django docs, that's best explained there. –  Surya Dec 17 '13 at 8:35
using AUTH_USER_MODEL assumes you are building all the stuff on your own from scratch and nothing else.. There are some small security measures these classes take care of.. take a look into that code when you are building from scratch –  Surya Dec 17 '13 at 8:37
Yeah, I understood that. But now that I am not using AbstractBaseUser, but AbstractUser, I don't need to build from scratch. And yet, user creation in the usual method creates the default user rather than my custom one. –  user1349663 Dec 17 '13 at 8:38
add comment

Your Answer


