django.core.exceptions.ValidationError:[“'urvi'值必须是整数。”]

时间:2019-06-08 11:00:18

标签: python django exception django-models

尝试访问管理面板时出现错误。它说在/ admin / [“'urvi'的值必须是整数。”处的ValidationError。

我已经尝试使用id并自动对其进行递增,但是没有任何效果。

Models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.
Roles = (
    ('sales', 'SALES'),
    ('operations', 'OPERATIONS'),
    ('cashier', 'CASHIER'),
    ('frontdesk', 'FRONTDESK'),
    ('admin', 'ADMIN'),
    ('client', 'CLIENT'),

)


class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE,              default='none')
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField(max_length=100, unique=True)
    password = models.CharField(max_length=50)
    username = models.CharField(max_length=50)
    role = models.CharField(max_length=50, choices=Roles,  default='client')

    def __str__(self):
        return self.username

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from NewApp import views


urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^$',views.register,name='register'),

1 个答案:

答案 0 :(得分:2)

userOneToOneField,您提供的默认值是字符串! ORM如何知道如何将该字符串转换为User实例?您可以在此处提供一个int,这是有问题的,因为您要确保数据库中存在具有该ID的User。或者-也许就是您想要的-使该字段为可空(null=True)并将默认值设置为None(而不是'none')。