Myzel394
Myzel394

Reputation: 1317

Django - Migration doesn't work and strange error

I'm new to django. I'm trying to create a custom user model but everytime I try to migrate I get a strange error. Maybe you can help me. Here are my files:

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

Now whenever I try to migrate I get this error:

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

and everything is added to the database except "ip_address"

I also cant login the user... I don't know what's wrong but I would be happy about some help.

enter image description here

EDIT: I added a OneToOne relation (Thank you Bidhan Majhi) and I had to delete my old migrations. Now it's working. Thank you!

Upvotes: 0

Views: 171

Answers (1)

Bidhan Majhi
Bidhan Majhi

Reputation: 1370

User model you have called has already username, first_name, last_name, password and email fields. If you want to create a new User model completely, you can go for AbstractUser, else the better choice is OneToOne relation with User model,

class UserProfile(models.Model):
      user = models.OneToOneField(User, on_delete = models.CASCADE)
      #rest of your fields

In your models's ip_address, add null=True, as you are not requesting ip_address in your form. Or you can add ip_address in your form's field.

Upvotes: 2

Related Questions