0

I created a profile model that contains additional fields for the User model. It works as expected, but when I add a new user, the added fields appear on both the "Add user" page (first page) and the "Change user" page (second page). How can I prevent my added fields from appearing on the "Add user" page?

models.py

from django.db import models
from django.contrib.auth.models import User

class Member(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    my_added_field = models.CharField(max_length=15)

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User

from .models import Member

class MemberInline(admin.StackedInline):
    model = Member

class MemberAdmin(UserAdmin):
    inlines = (MemberInline,)

admin.site.unregister(User)
admin.site.register(User, MemberAdmin)
Kamlesh
  • 2,032
  • 2
  • 19
  • 35
Mike
  • 150
  • 1
  • 8

1 Answers1

0

You need to use exclude = 'your-field-name' in Meta 'your-field-name' is field which you not want to show in admin section.

SuReSh
  • 1,503
  • 1
  • 22
  • 47
  • Do I need to extend UserCreationForm, and exclude the field there? – Mike Mar 22 '16 at 04:21
  • No, you need to put this code in serializer class Meta: exclude = 'you-field-name' – SuReSh Mar 22 '16 at 04:49
  • I'm new to web development and Django, can you provide a link where I can read more about this? The information I found on djangoproject.com describes using serializers for translating models to other formats (e.g. json, xml), and for creating custom fields. Also, I'm wondering if this approach will exclude my added fields from all forms? I want to exclude them only from the UserCreationForm. – Mike Mar 22 '16 at 23:29