将数据保存成两个table。 ID 为“None”的学生不存在。也许它被删除了?

Save data into two table. Student with ID “None” doesn’t exist. Perhaps it was deleted?

我尝试将数据保存到标准用户 class 和我的学生 class。但是我有一个问题。

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 的新手,所以如果你能帮助我,我将不胜感激 非常感谢

我做到了。我只需要这样做。我稍微修改了我的代码。

    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')

@dannycrief 出现“..id with None doesn't exist”的错误是因为django模型需要一个主键来识别记录,因此自动添加了一个“models.Autofield( primary_key = True)”出于同样的目的。

当我们得到上述错误时,同样表明django 无法获取主键。我们可以通过在我们的字段之一中添加 primary_key+True 来解决它。