尝试手动保存用户时python django中的错误

时间:2019-03-19 20:58:10

标签: python django django-models

当我尝试在Django中手动创建新用户时,我正在尝试一些错误

这是我的代码:

x <- structure(list(Cust_ID = c(10, 11, 11, 10, 10, 11, 10, 10, 11, 
11, 10, 11, 10, 11, 10, 11, 11, 11, 11, 10, 10, 11, 11, 10, 11, 
11, 11, 11, 10, 11, 11, 11, 11, 11, 10, 11, 10, 10, 10, 10, 11, 
10, 11, 11, 11, 10, 10, 10, 10, 11, 10, 10, 10, 10, 11, 11, 11, 
11, 11, 11, 11, 10, 11, 11, 10, 11, 11, 11, 10, 11, 11, 10, 10, 
10, 11, 11, 11, 11, 10, 11, 11, 11, 11, 10, 11, 11, 10, 11, 10, 
11, 10, 10, 11, 10, 10, 11, 11, 10, 11, 11, 11, 11, 10, 11, 11, 
10, 11, 11, 11, 11, 10, 11, 11, 10, 11, 10, 10, 10, 11, 11, 10, 
11, 10, 11, 10, 11, 11, 11, 11, 11, 11, 10, 11, 11, 11, 10, 10, 
11, 10, 11, 11, 11, 10, 11, 10, 10, 10, 10, 11, 10, 11, 11, 10, 
10, 11, 11, 10, 11, 10, 11, 10, 11, 10, 10, 10, 11, 11, 10, 10, 
11, 10, 11, 11, 10, 10, 11, 10, 11, 11, 10, 10, 11, 11, 11, 10, 
11, 10, 11, 10, 10, 11, 11, 11, 11, 11, 10, 11, 10, 11, 11), 
    ID_code = c(8003, 8002, 8002, 8001, 8003, 8003, 8001, 8001, 
    8002, 8003, 8001, 8003, 8002, 8005, 8004, 8001, 8002, 8004, 
    8001, 8001, 8002, 8003, 8001, 8002, 8003, 8005, 8002, 8001, 
    8005, 8004, 8001, 8003, 8004, 8005, 8002, 8004, 8002, 8003, 
    8003, 8003, 8003, 8002, 8001, 8003, 8003, 8001, 8004, 8001, 
    8002, 8001, 8002, 8003, 8002, 8003, 8001, 8003, 8005, 8001, 
    8003, 8005, 8002, 8002, 8004, 8002, 8005, 8002, 8003, 8001, 
    8001, 8003, 8004, 8001, 8001, 8003, 8004, 8003, 8001, 8001, 
    8001, 8005, 8005, 8003, 8003, 8002, 8005, 8002, 8001, 8002, 
    8002, 8001, 8003, 8003, 8002, 8002, 8001, 8003, 8002, 8001, 
    8001, 8005, 8001, 8001, 8004, 8003, 8003, 8003, 8002, 8004, 
    8002, 8003, 8001, 8001, 8002, 8002, 8001, 8005, 8004, 8001, 
    8004, 8005, 8003, 8002, 8001, 8001, 8001, 8001, 8004, 8001, 
    8004, 8001, 8002, 8004, 8003, 8003, 8002, 8002, 8001, 8001, 
    8003, 8004, 8003, 8003, 8002, 8001, 8003, 8002, 8002, 8003, 
    8003, 8003, 8004, 8002, 8005, 8003, 8003, 8005, 8004, 8003, 
    8004, 8004, 8003, 8002, 8003, 8004, 8003, 8001, 8005, 8005, 
    8005, 8004, 8002, 8001, 8003, 8003, 8003, 8004, 8003, 8005, 
    8005, 8001, 8003, 8003, 8003, 8003, 8001, 8001, 8005, 8001, 
    8002, 8005, 8002, 8001, 8005, 8002, 8005, 8005, 8001, 8003, 
    8003, 8002), price = c(710, 249000, 360000, 2200, 1400, 290000, 
    575, 750, 1e+06, 189000, 1500, 345000, 3300, 429000, 980, 
    195000, 368000, 435000, 1500000, 845, 775, 180000, 832020, 
    1250, 199000, 388000, 410000, 148500, 2100, 420000, 458000, 
    358000, 220000, 770000, 1550, 330000, 2100, 1650, 2500, 1450, 
    175000, 1700, 169500, 174000, 575000, 1200, 1050, 860, 1280, 
    565000, 2800, 1500, 10000, 1200, 239000, 212000, 690000, 
    470000, 350000, 950000, 750000, 1100, 239900, 480000, 1250, 
    485000, 225000, 210000, 1200, 425000, 320000, 1195, 1900, 
    1700, 420000, 148000, 239000, 275000, 2000, 330000, 225000, 
    198000, 360000, 2100, 279000, 525000, 900, 410000, 1200, 
    165000, 950, 4500, 269000, 1250, 900, 330000, 350000, 1250, 
    289000, 7e+05, 109000, 135000, 1200, 320000, 195000, 1600, 
    372000, 170000, 1050000, 522500, 1100, 218000, 117000, 1600, 
    355000, 5400, 1700, 2250, 257000, 790000, 750, 525000, 1650, 
    298000, 1045, 490000, 237500, 169000, 265000, 169000, 375000, 
    1000, 340000, 610000, 6e+05, 2000, 1000, 451000, 1400, 189000, 
    398000, 198000, 1400, 195000, 1200, 1600, 675, 790, 179000, 
    1500, 480000, 605000, 1600, 900, 355000, 712066, 900, 345000, 
    925, 495000, 2100, 349000, 1250, 900, 1700, 590000, 660000, 
    950, 2500, 560000, 1190, 340000, 270000, 1275, 1475, 252000, 
    1120, 680000, 245000, 2100, 1300, 395000, 525000, 398000, 
    1100, 280000, 1400, 135000, 4950, 950, 5e+05, 370000, 470000, 
    253000, 399000, 1600, 270000, 1400, 267000, 2980000), mean_cust_price = c(1492.8, 
    581826.086956522, 581826.086956522, 1310.47619047619, 1492.8, 
    314078.125, 1310.47619047619, 1310.47619047619, 581826.086956522, 
    314078.125, 1310.47619047619, 314078.125, 2191, 544816.625, 
    1081.875, 344300.666666667, 581826.086956522, 330650, 344300.666666667, 
    1310.47619047619, 2191, 314078.125, 344300.666666667, 2191, 
    314078.125, 544816.625, 581826.086956522, 344300.666666667, 
    1972.22222222222, 330650, 344300.666666667, 314078.125, 330650, 
    544816.625, 2191, 330650, 2191, 1492.8, 1492.8, 1492.8, 314078.125, 
    2191, 344300.666666667, 314078.125, 314078.125, 1310.47619047619, 
    1081.875, 1310.47619047619, 2191, 344300.666666667, 2191, 
    1492.8, 2191, 1492.8, 344300.666666667, 314078.125, 544816.625, 
    344300.666666667, 314078.125, 544816.625, 581826.086956522, 
    2191, 330650, 581826.086956522, 1972.22222222222, 581826.086956522, 
    314078.125, 344300.666666667, 1310.47619047619, 314078.125, 
    330650, 1310.47619047619, 1310.47619047619, 1492.8, 330650, 
    314078.125, 344300.666666667, 344300.666666667, 1310.47619047619, 
    544816.625, 544816.625, 314078.125, 314078.125, 2191, 544816.625, 
    581826.086956522, 1310.47619047619, 581826.086956522, 2191, 
    344300.666666667, 1492.8, 1492.8, 581826.086956522, 2191, 
    1310.47619047619, 314078.125, 581826.086956522, 1310.47619047619, 
    344300.666666667, 544816.625, 344300.666666667, 344300.666666667, 
    1081.875, 314078.125, 314078.125, 1492.8, 581826.086956522, 
    330650, 581826.086956522, 314078.125, 1310.47619047619, 344300.666666667, 
    581826.086956522, 2191, 344300.666666667, 1972.22222222222, 
    1081.875, 1310.47619047619, 330650, 544816.625, 1492.8, 581826.086956522, 
    1310.47619047619, 344300.666666667, 1310.47619047619, 344300.666666667, 
    330650, 344300.666666667, 330650, 344300.666666667, 581826.086956522, 
    1081.875, 314078.125, 314078.125, 581826.086956522, 2191, 
    1310.47619047619, 344300.666666667, 1492.8, 330650, 314078.125, 
    314078.125, 2191, 344300.666666667, 1492.8, 2191, 2191, 1492.8, 
    314078.125, 1492.8, 330650, 581826.086956522, 1972.22222222222, 
    1492.8, 314078.125, 544816.625, 1081.875, 314078.125, 1081.875, 
    330650, 1492.8, 581826.086956522, 1492.8, 1081.875, 1492.8, 
    344300.666666667, 544816.625, 1972.22222222222, 1972.22222222222, 
    330650, 2191, 344300.666666667, 314078.125, 1492.8, 1492.8, 
    330650, 1492.8, 544816.625, 544816.625, 1310.47619047619, 
    1492.8, 314078.125, 314078.125, 314078.125, 1310.47619047619, 
    344300.666666667, 1972.22222222222, 344300.666666667, 2191, 
    1972.22222222222, 581826.086956522, 344300.666666667, 544816.625, 
    581826.086956522, 544816.625, 1972.22222222222, 344300.666666667, 
    1492.8, 314078.125, 581826.086956522)), .Names = c("Cust_ID", 
"ID_code", "price", "mean_cust_price"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -200L), vars = c("Cust_ID", 
"ID_code"), labels = structure(list(Cust_ID = c(10, 10, 10, 10, 
10, 11, 11, 11, 11, 11), ID_code = c(8001, 8002, 8003, 8004, 
8005, 8001, 8002, 8003, 8004, 8005)), row.names = c(NA, -10L), class = "data.frame", vars = c("Cust_ID", 
"ID_code"), drop = TRUE, .Names = c("Cust_ID", "ID_code")), indices = list(
    c(3L, 6L, 7L, 10L, 19L, 45L, 47L, 68L, 71L, 72L, 78L, 86L, 
    94L, 97L, 110L, 117L, 122L, 124L, 136L, 179L, 184L), c(12L, 
    20L, 23L, 34L, 36L, 41L, 48L, 50L, 52L, 61L, 83L, 88L, 93L, 
    113L, 135L, 142L, 145L, 146L, 170L, 188L), c(0L, 4L, 37L, 
    38L, 39L, 51L, 53L, 73L, 90L, 91L, 105L, 120L, 138L, 144L, 
    147L, 149L, 153L, 160L, 162L, 164L, 173L, 174L, 176L, 180L, 
    197L), c(14L, 46L, 102L, 116L, 131L, 156L, 158L, 163L), c(28L, 
    64L, 115L, 152L, 167L, 168L, 186L, 189L, 195L), c(15L, 18L, 
    22L, 27L, 30L, 42L, 49L, 54L, 57L, 67L, 76L, 77L, 89L, 98L, 
    100L, 101L, 111L, 114L, 123L, 125L, 127L, 129L, 137L, 143L, 
    165L, 171L, 185L, 187L, 191L, 196L), c(1L, 2L, 8L, 16L, 26L, 
    60L, 63L, 65L, 85L, 87L, 92L, 96L, 106L, 108L, 112L, 121L, 
    130L, 134L, 151L, 161L, 190L, 193L, 199L), c(5L, 9L, 11L, 
    21L, 24L, 31L, 40L, 43L, 44L, 55L, 58L, 66L, 69L, 75L, 81L, 
    82L, 95L, 103L, 104L, 109L, 132L, 133L, 140L, 141L, 148L, 
    154L, 157L, 172L, 181L, 182L, 183L, 198L), c(17L, 29L, 32L, 
    35L, 62L, 70L, 74L, 107L, 118L, 126L, 128L, 139L, 150L, 159L, 
    169L, 175L), c(13L, 25L, 33L, 56L, 59L, 79L, 80L, 84L, 99L, 
    119L, 155L, 166L, 177L, 178L, 192L, 194L)), drop = TRUE, group_sizes = c(21L, 
20L, 25L, 8L, 9L, 30L, 23L, 32L, 16L, 16L), biggest_group_size = 32L)

该代码能够正确插入用户,我在数据库中看到了更改,但是使用此命令保存用户后,代码中断了

from django.contrib.auth.models import User

newuser = User()
newuser.username = request.data['username']
newuser.first_name = request.data['first_name']
newuser.last_name = request.data['last_name']
newuser.email = request.data['email']
newuser.is_active = True
newuser.set_password(request.data['password'])
newuser.full_clean()
newuser.save()

我真的不知道是什么问题

2 个答案:

答案 0 :(得分:0)

改为使用该用户

from django.contrib.auth import get_user_model
User = get_user_model()

https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#django.contrib.auth.get_user_model

答案 1 :(得分:0)

我解决了添加此代码的问题

args = ()
kwargs = {}
newuser.save(*args,**kwargs)

我想到了这篇文章 Django - TypeError - save() got an unexpected keyword argument 'force_insert'

事实上,我检测到我所有的表格也都失败了,例如我的一张表格

class UserRegisterForm(UserCreationForm):


    class Meta:
        model = User
        fields = ['first_name','last_name','username','email','password1','password2']

直到今天失败为止一切正常,所以我需要将其更改为

class UserRegisterForm(UserCreationForm):


    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)**

    class Meta:
        model = User
        fields = ['first_name','last_name','username','email','password1','password2']

我不知道什么时候开始这个问题,我的意思是什么django版本。