Я новичок в Джанго.Я пытаюсь создать пользовательскую модель, но каждый раз, когда я пытаюсь выполнить миграцию, я получаю странную ошибку.Возможно ты можешь помочь мне.Вот мои файлы:
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"
Я также не могу войти в систему пользователя ... Я не знаю, что не так, но я был бы рад некоторой помощи.
РЕДАКТИРОВАТЬ: Я добавил отношение OneToOne (спасибо Бидхан Маджхи), и мне пришлось удалить мои старые миграции.Теперь это работает.Спасибо!