将数据保存到两个表中。 ID 为“无”的学生不存在。也许它被删除了?
Posted
技术标签:
【中文标题】将数据保存到两个表中。 ID 为“无”的学生不存在。也许它被删除了?【英文标题】:Save data into two table. Student with ID “None” doesn’t exist. Perhaps it was deleted? 【发布时间】:2020-07-31 13:38:30 【问题描述】:我尝试将数据保存到标准用户类和我的学生类。但我有一个问题。
ID 为“无”的学生不存在。也许它被删除了? 我真的不知道如何保存这些数据。
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 的新手,如果你能帮助我,我将不胜感激 非常感谢
【问题讨论】:
【参考方案1】:@dannycrief,出现“..id with None doesn't exist”的错误是因为 django 模型需要一个主键来识别记录,因此会自动添加一个“models.Autofield(primary_key = True)”目的。
当我们得到上述错误时,同样表明 django 无法获取主键。我们可以通过在其中一个字段中添加 primary_key+True 来解决它。
【讨论】:
为时已晚,但感谢您的回答 Rahul!【参考方案2】:我做到了。我只需要这样做。我稍微修改了我的代码。
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')
【讨论】:
以上是关于将数据保存到两个表中。 ID 为“无”的学生不存在。也许它被删除了?的主要内容,如果未能解决你的问题,请参考以下文章