0

Implementing custom user for my project,
here is my account/models.py

class myUser(AbstractBaseUser, PermissionsMixin):
    #blah
    date_joined = models.DateTimeField(auto_now_add=True)
    #blah

and my account/admin.py

class myUserDetail(admin.ModelAdmin):
    list_display = ('email','password','is_active','is_staff','date_joined',)
    fields = list_display    

admin.site.register(myUser, myUserDetail)

The list_display works fine, but when I click into a user, error is raised : Unknown field(s) (date_joined) specified for myUser. Check fields/fieldsets/exclude attributes of class myUserDetail.

In fact it exists in postgres...
Please help!

taesu
  • 4,482
  • 4
  • 23
  • 41

2 Answers2

1

The error is being triggered when the ModelForm is created automatically for the admin, specifically if there are missing fields. Because you are using auto_now_add=True, which implicitly sets editable=False, the field cannot be included in the automatically generated form. Because of this, an error is triggered.

I would recommend specifying fields and list_display independently, as they aren't actually the same.

Community
  • 1
  • 1
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
  • Makes perfect sense. But can I explicitly specify `date_joined = models.DateTimeField(auto_now_add=True, editable=True)` – taesu May 16 '15 at 03:42
  • 1
    @taesu Even if Django doesn't trigger an error, [it wouldn't have any effect](https://github.com/django/django/blob/3226050358cfcffcb14f7e48e3a5cee873388406/django/db/models/fields/__init__.py#L1181). – Kevin Brown-Silva May 16 '15 at 03:55
  • 1
    thanks. you not only answered me the question, but taught me to search these within the project itself. – taesu May 16 '15 at 04:09
-1

When you use a custom User model in Django you should add the following line to your settings:

AUTH_USER_MODEL = 'accounts.MyUser'

I also think you should subclass from AbstractUser which is an abstract base class implementing a fully featured User model with admin-compliant permissions, and includes email, date_joined, etc. fields.

You can read more about customizing the user model in the Django documentation.

Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
Eric Acevedo
  • 1,182
  • 1
  • 11
  • 26
  • I have set the AUTH_USER_MODEL , and my choice of AbstractBaseUser over AbstractUser is intentional. Im sorry but this does not answer my question. – taesu May 16 '15 at 03:20
  • which part of the code do you require to see? I thought I've supplied enough block of code.. hm.. – taesu May 16 '15 at 03:29