我有一个表单,当用户提交表单时,我需要当前登录的用户自动填充到 django 管理员中。我希望当前用户的用户名填充在它User
在 django 管理员中所说的列中。附上截图。
如何使用当前代码正确执行该操作?
from users.models import CustomUser
如果有帮助,我正在使用的自定义用户模型位于。
我很高兴得到任何帮助,干杯
user_profile/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from user_profile.forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from user_profile.models import Listing
from 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/models
from django.contrib import auth
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, UserManager
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from users.models import CustomUser
相关分类