I want to create a user profile model as an extension of the original django user model. I implemented the model in models.py:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
solved = models.IntegerField(default=0)
profile_pic = models.ImageField(upload_to='profile_pic', default="/media/profile_pic/default-profile.jpg")
school = models.CharField(max_length=200, blank=True, null=True)
However, when I create a new user in the admin site, there is no default value for the profile_pic field. In addition, although the solved field shows a '0' on the form, the value is not saved after I pressed save:
I registered the extended user model like this in admin.py:
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'User Profile'
class UserAdmin(BaseUserAdmin):
list_display = ('username', 'is_staff', 'solved')
inlines = (UserProfileInline,)
def solved(self, obj):
return obj.userprofile.solved
admin.site.register(User, UserAdmin)
Am I doing something wrong? Is the default value only used when creating a user using python but not the admin site? Please help. Thanks.
profile_pic
at the time of object creation.profile_pic
andsolved
are still left blank.