将数据保存到两个表中。编号为“ None”的学生不存在。也许它被删除了?

问题描述 投票:0回答:1

我试图将数据保存到标准User类和Student类中。但是我有一个问题。

ID为“ None”的学生不存在。也许它被删除了?我真的不知道如何保存这些数据。

models.py

class Student(models.Model):
    studentIndex = models.IntegerField(unique=True)
    studentName = models.CharField(max_length=50)
    studentSurname = models.CharField(max_length=50, default='')
    studentPhoneNumber = models.CharField(max_length=12, default='+48')
    studentPesel = models.CharField(max_length=11, default='')
    studentStudyMode = models.CharField(max_length=12, default='')
    studentFaculty = models.CharField(max_length=30, default='')
    studentSemester = models.IntegerField(default='')
    studentAddress = models.CharField(max_length=255, default='')
    studentMotherName = models.CharField(max_length=100, default='')
    studentBirth = models.DateField(help_text='year-month-day')
    studentEmail = models.EmailField(max_length=255, default='')
    studentImage = models.ImageField(default='default.jpg', upload_to='profile_pics')
    studentPassword = models.CharField(max_length=100, default='12345678',
                                       help_text='Do not modify this field. Password will be generated automatically')
    studentDateJoined = models.DateTimeField(default=now)

    def save(self, *args, **kwargs):
        default_password = str(self.studentBirth)[0:4] + str(self.studentBirth)[5:7] + \
                           str(self.studentBirth)[8:10] +\
                           self.studentMotherName[0] +\
                           lower(self.studentName[0])
        User.objects.create_user(username=self.studentIndex, email=self.studentEmail, password=default_password,
                                 first_name=self.studentName, last_name=self.studentSurname)

    def __str__(self):
        return f'{self.studentIndex} - {self.studentName} {self.studentSurname}'

当我尝试这个时

...
    def save(self, *args, **kwargs):
        default_password = str(self.studentBirth)[0:4] + str(self.studentBirth)[5:7] + \
                           str(self.studentBirth)[8:10] +\
                           self.studentMotherName[0] +\
                           lower(self.studentName[0])
        Student.objects.create() # <- this one
        User.objects.create_user(username=self.studentIndex, email=self.studentEmail, password=default_password,
                                 first_name=self.studentName, last_name=self.studentSurname)

我有字符串索引超出范围错误(IndexError)或比较中超出了最大递归深度(RecursionError)

我是Django的新手,如果您能帮助我,我将不胜感激非常感谢

django django-models django-admin django-database
1个回答
0
投票

我做到了。我需要做的所有事情。我修改了一点代码。

    def save(self, *args, **kwargs):
        try:
            default_password = str(self.studentBirth)[0:4] + str(self.studentBirth)[5:7] + \
                               str(self.studentBirth)[8:10] +\
                               self.studentMotherName[0] +\
                               lower(self.studentName[0])
            Student.objects.create()
            User.objects.create_user(username=self.studentIndex, email=self.studentEmail, password=default_password,
                                     first_name=self.studentName, last_name=self.studentSurname)
            super(Student, self).save() # <- this is my solve
        except IndexError:
            print('IndexError')
        except RecursionError:
            print('RecursionError')
© www.soinside.com 2019 - 2024. All rights reserved.