Django - 登录用户未在 admin.py 中填充 [英] Django - Logged in User Not Populating in admin.py
问题描述
我正在尝试创建一个表单,当当前登录用户提交时,admin.py
中的 user
列将填充登录用户.
I’m trying to create a form to when the current logged in user makes a submission the user
column in admin.py
gets populated with the logged in user.
我的问题:当使用 CustomUserCreationForm
创建新用户时,会填充 user
列,但是当新创建的用户使用下面列出的表单提交表单时,user
列未填充.
My problem:
The user
column gets populated when a new user gets created using the CustomUserCreationForm
however when the newly created user makes a form submission with the form listed below, the user
column doesn’t get populated.
我试图从中获取用户名的自定义用户模型位于 from users.models import CustomUser
中,所以我不确定为什么这不起作用.
The Custom User Model that I'm trying to get the username from is located in from users.models import CustomUser
so I’m not sure why this isn’t working.
如何使用下面列出的表单将当前登录的用户填充到 users
列中的 admin.py
中?
How do I get the current logged in user to populate in admin.py
in the users
column with the form listed below?
我很高兴得到任何帮助,谢谢.
Any help i gladly appreciated, thanks.
代码如下:
from django.db import models
from django.urls import reverse
from django.contrib.auth.models import AbstractUser, UserManager
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
class Listing (models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
zip_code = models.CharField(max_length=100)
mobile_number = models.CharField(max_length=100)
cc_number = models.CharField(max_length=100)
cc_expiration = models.CharField(max_length=100)
cc_cvv = models.CharField(max_length=100)
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = Listing.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=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', 'user']
list_filter = ['name', 'zip_code', 'created', 'updated', 'user']
admin.site.register(Listing, UserProfileAdmin)
user_profile/views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.conf import settings
from .forms import HomeForm
from users.forms import CustomUserCreationForm, CustomUserChangeForm
from .models import Listing
from users.models import CustomUser
from django.urls import reverse_lazy
# add to your views
def change_view(request):
form = HomeForm(request.POST or None)
user_profile = Listing.objects.all
if form.is_valid():
form.save()
form = HomeForm()
context = {
'form': form, 'user_profile': user_profile
}
return render(request, "myaccount.html", context)
user_profile/forms.py
import os
from django import forms
from django.forms import ModelForm
from django.forms import widgets
from django.utils import six
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
from avatar.conf import settings
from avatar.models import Avatar
from .models import Listing
class HomeForm(forms.ModelForm):
user = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
username = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
created = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
name = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Full Name', 'class': 'form-control'}))
address = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Address', 'class': 'form-control'}))
zip_code = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Zipcode', 'class': 'form-control'}))
mobile_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Mobile Number', 'class': 'form-control'}))
cc_number = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Credit Card', 'class': 'form-control'}))
cc_expiration = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Expiration Date', 'class': 'form-control'}))
cc_cvv = forms.CharField(required=False, label='', max_length=100, widget=forms.TextInput(attrs={'placeholder': 'CVV', 'class': 'form-control'}))
class Meta:
model = Listing
fields = '__all__'
settings.py
AUTH_USER_MODEL = 'users.CustomUser'
推荐答案
试试这个
forms.py
import os
from django import forms
from django.forms import ModelForm
from django.forms import widgets
from django.utils import six
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import filesizeformat
from avatar.conf import settings
from avatar.models import Avatar
from .models import Listing
class HomeForm(forms.ModelForm):
class Meta:
model = Listing
fields = ('name', 'address', 'zip_code', 'mobile_number', 'cc_number', 'cc_number', 'cc_expiration', 'cc_cvv')
在模型文件中添加
class ListingManager(models.Manager):
def save_from_object(self, request, obj):
obj.user = request.user
obj.save()
使用 objects
作为新经理添加更新您的列表模型
Add Update your Listing Model with objects
as new manager
class Listing (models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=True)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
zip_code = models.CharField(max_length=100)
mobile_number = models.CharField(max_length=100)
cc_number = models.CharField(max_length=100)
cc_expiration = models.CharField(max_length=100)
cc_cvv = models.CharField(max_length=100)
objects = ListingManager()
这应该可行,而且我不确定您将用户保留为 OneToOneField 的想法(这将不允许具有相同用户实例的多个记录),我认为您需要将其更改为 ForeignKey.参考 django OneToOneField 和有什么区别外键?
This should work, Also I am not sure about your idea of keeping user as OneToOneField (This will not allow having multiple records with same user instance), I think you need to change it to ForeignKey. Refer What's the difference between django OneToOneField and ForeignKey?
这篇关于Django - 登录用户未在 admin.py 中填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!