我有一个表单,当用户提交表单时,我需要当前登录的用户自动填充到 django 管理员中。我希望当前用户的用户名填充在它User在 django 管理员中所说的列中。附上截图。如何使用当前代码正确执行该操作?from users.models import CustomUser如果有帮助,我正在使用的自定义用户模型位于。我很高兴得到任何帮助,干杯user_profile/admin.pyfrom django.contrib import adminfrom django.contrib.auth import get_user_modelfrom django.contrib.auth.admin import UserAdminfrom user_profile.forms import HomeFormfrom users.forms import CustomUserCreationForm, CustomUserChangeFormfrom user_profile.models import Listingfrom users.models import CustomUser# Register models here.class UserProfileAdmin(admin.ModelAdmin): list_display = ['name', 'address', 'zip_code', 'mobile_number', 'created', 'updated', 'get_username'] list_filter = ['name', 'zip_code', 'created', 'updated',] def get_username(self, obj): if obj.user is not None: return obj.user.username return '-' get_username.short_description = 'User'admin.site.register(Listing, UserProfileAdmin)#user_profile/modelsfrom django.contrib import authfrom django.db import modelsfrom django.urls import reversefrom django.contrib.auth.models import AbstractUser, UserManagerfrom django.contrib.auth.models import BaseUserManagerfrom django.contrib.auth.models import Userfrom django.db.models.signals import post_savefrom django.dispatch import receiverfrom django.conf import settingsfrom users.forms import CustomUserCreationForm, CustomUserChangeFormfrom users.models import CustomUser
添加回答
举报
0/150
提交
取消