Note that there are some explanatory texts on larger screens.

plurals
  1. POFor the django admin, how do I add a field to the User model and have it editable in the admin?
    primarykey
    data
    text
    <p>I'm trying to understand the django admin better and at the same time, I'm trying to add one more field to the current user admin. In models.py I've done</p> <pre><code>User.add_to_class('new_field', models.BooleanField(default=False)) </code></pre> <p>and in admin.py I've got the following (with fieldsets basically just copied from django/contrib/auth/admin.py)</p> <pre><code>class AdjUserAdmin(UserAdmin): list_display = UserAdmin.list_display + ('new_field',) list_filter = UserAdmin.list_filter + ('new_field',) fieldsets = UserAdmin.fieldsets fieldsets[1][1]['fields'] = ('first_name','last_name','email','new_field') </code></pre> <p>The problem is, when I do this I get the error:</p> <p><strong><code>AdjUserAdmin.fieldsets[4][1]['fields']' refers to field 'new_field' that is missing from the form.</code></strong></p> <p>I've looked at UserChangeForm, but it looks like it's already correctly pulling in User as the model. I'm not sure as to why <code>new_field</code> is missing from the form.</p> <p>Thanks</p> <p><strong>In regards to this being smelly code</strong></p> <p>I know this is a smelly monkey patching way to go about doing this, but subclassing gives me issues mainly for <a href="http://www.b-list.org/weblog/2007/feb/20/about-model-subclassing/" rel="noreferrer">these</a> reasons.. if I could get it to work the way stated above, I'd be happy.. and maybe smelly.</p> <p><strong>In regards to the recommended way</strong></p> <p>I'm aware of the recommended way of creating a user profile, just that in particular situations, I don't see the merit in creating an entire new table and having an additional call to the database when all I want to store is an extra bit of information such as <code>is_private</code> or some such. If I'm storing lots more info, then I agree, setting up a user profile is preferable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload