Django-管理员未填充OneToOneField

时间:2019-01-02 01:35:37

标签: python django django-models django-forms

我正在尝试使用OneToOneField获取当前登录用户的用户名,以便在用户提交表单后在管理员中进行填充。

用户名应放在user的{​​{1}}列中。

我尝试了各种方法,但还是没有运气。我对此并不陌生,这是我正在构建的第一个Django应用程序,因此我不确定自己缺少什么。

我被困住了,不知道自己在做什么,因此非常感谢您的帮助。

有人可以帮忙吗?我想念什么?

谢谢!

enter image description here

下面的代码:

user_profile / models

admin.py

user_profile / admin.py

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.CASCADE, null=True)
    created =  models.DateTimeField(auto_now_add=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=User)

master_application / settings.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 user_profile.models import Listing

# 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)
#admin.site.unregister(Listing)

users / models.py

AUTH_USER_MODEL = 'users.CustomUser'
AUTH_PROFILE_MODULE = 'users.UserProfile'

1 个答案:

答案 0 :(得分:2)

您的信号中断; kwargs永远不会拥有from selenium.webdriver.chrome.webdriver import WebDriver from selenium import webdriver import time def next(): giveawayPage = 1 currentPageURL = 'https://www.amazon.com/ga/giveaways?pageId=' + str(giveawayPage) while True: try: nextButton = driver.find_element_by_xpath('//*[@id="giveawayListingPagination"]/ul/li[7]') except: nextPageStatus = 'false' print('false') else: nextpageStatus = 'true' giveawayPage = giveawayPage + 1 driver.get(currentPageURL) if (nextPageStatus == 'false'): break if __name__ == '__main__': driver = webdriver.Chrome('./chromedriver') driver.get('https://www.amazon.com/ga/giveaways?pageId=1') next() 键,因此将永远不会创建配置文件。您实际要做的是检查创建时(而不是更新时)正在调用信号,添加然后创建Listing的实例:

user

请注意,AUTH_PROFILE_MODULE设置已使用多年,您应该将其删除。