0

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: enter image description here

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.

2
  • Django will use the default value of profile_pic at the time of object creation.
    – JPG
    Commented May 10, 2021 at 7:08
  • @JPG hmm, but it doesn't seem like it when I created a new user. The fields profile_pic and solved are still left blank.
    – blastzit
    Commented May 10, 2021 at 8:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.