Django-迁移无法正常工作,并且出现奇怪的错误

时间:2018-10-04 16:06:22

标签: python sql django login

我是django的新手。 我正在尝试创建自定义用户模型,但是每次尝试迁移时,都会遇到一个奇怪的错误。 也许你可以帮我。这是我的文件:

models.py:

from django.db import models
from django.contrib.auth.models import User
from datetime import date, datetime


class User(models.Model):
username = models.CharField(max_length=31, unique=True)
first_name = models.CharField(max_length=31, unique=True)
last_name = models.CharField(max_length=31, unique=True)
password = models.CharField(max_length=1023)
email = models.CharField(max_length=255, unique=True, primary_key=True)
typeOfPerson = models.CharField(max_length=15, default="G")
birth_date = models.DateField()
registration_date = models.DateTimeField(
    #default=str(date.today()) + " " + str(datetime.now())
    auto_now_add=True
)
ip_address = models.CharField(max_length=31, blank=True)

classNumber = models.CharField(max_length=2, blank=True)
shortName = models.CharField(max_length=3, blank=True)

rank = models.CharField(max_length=15, default="Normal")

forms.py:

from django import forms
from .models import User
from django.contrib.auth.hashers import make_password


class SignUpForm(forms.ModelForm):
typeOfPerson = forms.ChoiceField(label="Was bist du?", required=True, 
choices=(
    ("S", "Schüler"),
    ("L", "Lehrer"),
    ("E", "Elternteil"),
    ("G", "Gast"),
), widget=forms.Select(attrs={
    "id": "select",
}))

username = forms.CharField(max_length=31, min_length=3, required=True, 
widget=forms.TextInput(attrs={
    "placeholder": "Benutzername",
    "id": "username",
}))

first_name = forms.CharField(max_length=31, min_length=3, required=True, 
widget=forms.TextInput(attrs={
    "placeholder": "Vorname",
    "id": "name",
}))

last_name = forms.CharField(max_length=31, min_length=2, required=True, 
widget=forms.TextInput(attrs={
    "placeholder": "Nachname",
    "id": "surname",
}))

email = forms.EmailField(max_length=255, min_length=6, required=True, 
 widget=forms.TextInput(attrs={
    "placeholder": "E-Mail",
    "id": "email",
}))

birth_date = forms.DateField(label="Geburtsdatum", required=True, 
widget=forms.DateInput(attrs={
    "id": "date",
    "type": "date"
}))

classNumber = forms.IntegerField(max_value=13, min_value=5, required=False, 
widget=forms.NumberInput(attrs={
    "placeholder": "Klassenstufe",
    "id": "classes",
}))

shortName = forms.CharField(min_length=2, max_length=2, required=False, 
widget=forms.TextInput(attrs={
    "placeholder": "Kürzel",
    "id": "shortName",
}))

password1 = forms.CharField(min_length=8, max_length=1023, required=True, 
widget=forms.PasswordInput(attrs={
    "placeholder": "Passwort",
    "id": "password"
}))

password2 = forms.CharField(min_length=8, max_length=1023, required=True, 
widget=forms.PasswordInput(attrs={
    "placeholder": "Passwort bestätigen",
    "id": "password_verification"
}))

class Meta:
    model = User
    fields = ('username', 'first_name', 'last_name', 'email', 'password1', 
'password2', "typeOfPerson", "classNumber", "birth_date")

def save(self, request, ip):
    user = User(
        first_name=self.cleaned_data.get("first_name").lower(),
        last_name=self.cleaned_data.get("last_name").lower(),
        password=make_password(self.cleaned_data.get("password1")),
        email=str(self.cleaned_data.get("email")).lower(),
        typeOfPerson=self.cleaned_data.get("typeOfPerson"),
        birth_date=self.cleaned_data.get("birth_date"),
        username=self.cleaned_data.get("username").lower(),

        classNumber=self.cleaned_data.get("classNumber"),
        shortName=self.cleaned_data.get("shortName").lower(),
        ip_address=ip
                )
    user.save()

def email_exists(self):
    try:
        User.objects.get(email__iexact=self.data["email"].lower())
        return True
    except User.DoesNotExist:
        return False

def username_exists(self):
    try:
        User.objects.get(username__iexact=self.data["username"].lower())
        return True
    except User.DoesNotExist:
        return False

def first_name_exists(self):
    try:
        User.objects.get(first_name__iexact=self.data["first_name"].lower())
        return True
    except User.DoesNotExist:
        return False

def last_name_exists(self):
    try:
        User.objects.get(last_name__iexact=self.data["last_name"].lower())
        return True
    except User.DoesNotExist:
        return False

def getInfosSTR(self, email):
    user = User.objects.get(email__iexact=email)
    return "(FIRST_NAME='" + user.first_name + "',LAST_NAME='" + 
user.last_name + "',EMAIL='" + user.email + "',TYPEOFPERSON='" + 
user.typeOfPerson + "',BIRTH_DATE='" + str(user.birth_date) + 
"',REGISTRATION_DATE='" + str(user.registration_date) + 
"',EXTRA_FIELDS[CLASSNUMBER='" + str(user.classNumber) + "',SHORTNAME='" + 
user.shortName + "'])"
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']

class Meta:
    ordering = ["email"]

def __str__(self):
    return self.first_name + " " + self.last_name + " [" + self.email + "], " 
+ str(self.birth_date) + " - " + self.typeOfPerson

现在,每当我尝试迁移时,都会出现此错误:

ValueError: invalid literal for int() with base 10: 'none'"

,所有内容都添加到数据库中,除了“ ip_address”

我也无法登录用户...我不知道出了什么问题,但是我很乐意提供帮助。

enter image description here

编辑: 我添加了一个OneToOne关系(谢谢Bidhan Majhi),我不得不删除旧的迁移。现在可以了。谢谢!

1 个答案:

答案 0 :(得分:1)

您调用的用户模型已经具有用户名,名字,姓氏,密码和电子邮件字段。如果要完全创建新的用户模型,则可以选择AbstractUser,否则更好的选择是与用户模型建立OneToOne关系,

'use strict';

const 
  WebSocket = require('ws'),
  SocksProxyAgent = require('socks-proxy-agent')

let agent = new SocksProxyAgent({
  protocol: 'socks4:',
  host: '182.23.40.194',
  port: 60456
})

let options = {
  perMessageDeflate: false,
  handshakeTimeout: 10000,
  headers: {
    'User-Agent': 'Test 0.0.1'
  },
  agent: agent
}

let w = new WebSocket('wss://echo.websocket.org', options)

w.on('open', () => {
  console.log('connection was opened')
})

w.on('close', (code, reason) => {
  console.log('connection was closed', code, reason)
})

w.on('error', err => {
  console.log('we got an error', err)
})

w.on('unexpected-response', (request, response) => {
  console.log('we got a response', response.statusCode, response.statusMessage)
})

w.on('message', data => {
  console.log('we got some data', data)
})

w.on('ping', data => {
  console.log('the server pinged us')
  w.pong('', true, true)
})

w.on('pong', data => {
  console.log('we are alive')
})

w.on('upgrade', res => {
  console.log('connnection was upgraded', res.headers)
})

在模型的ip_address中,添加null = True,因为您没有在表单中请求ip_address。或者,您也可以在表单字段中添加ip_address。

相关问题