我创建了一个UserProfile
方法,我希望与用户一起访问,但即使我已设置related_name
,它似乎也没有返回任何内容。我错过了什么?
models.py
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile_user")
avatar = models.ImageField(upload_to = 'static/img/avatars', default = 'static/imgs/avatars/avatar.jpeg')
def __str__(self):
return self.user.username
def __unicode__(self):
return self.user.username
admin.py
class ProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'UserProfile'
fk_name = 'user'
class CustomUserAdmin(UserAdmin):
inlines = (ProfileInline, )
def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(CustomUserAdmin, self).get_inline_instances(request, obj)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
模板
到目前为止,在模板中我尝试了{{ user.profile_user.avatar }}
,{{ request.user.profile_user.avatar }}