我遇到了Django最奇怪的问题。
我在一个模块的类中有一个类:
class picture(models.Model):
picture = models.ImageField(upload_to='ProfilePictures', blank = True)
class profilePictures(models.Model):
userName = models.CharField(max_length=30, primary_key=True)
profilePictureNumber = models.IntegerField()
profile1 = picture()
profile2 = picture()
profile3 = picture()
profile4 = picture()
profile5 = picture()
profile6 = picture()
现在,当我重新启动Django时(例如我进行了更新),我输入了通常的内容:
python testproject.py makemigrations
python testproject.py migrate
python testproject.py runserver
但是当在上一次运行中访问数据库中的属性时,我什么也得不到。例如,我添加了一张图片,重新启动了数据库,然后它告诉我没有图片。
我使用Navicat访问了实际的数据库,我发现自动创建了"图片"仍然存在于数据库中,但看起来Django删除了两个数据库行之间的链接(每行都是一个模型)。
我会假设有一个命令来继承这些数据,但我似乎无法找到它。
答案 0 :(得分:0)
您应该只有一个班级ProfilePicture
,因为个人资料图片编号没有任何意义。
class profilePictures(models.Model):
userName = models.CharField(max_length=30, primary_key=True)
picture = models.ImageField(upload_to='ProfilePictures', blank = True)
或者您可以使用Foreign Key
建立不同表之间的关系。 Foreign Key
的文档位于Foreign Key
Example
class Person(models.Model):
name = models.CharField(max_length=50)
birthday = models.DateField()
anniversary = models.ForeignKey(Anniversary)
address = models.ForeignKey(Address)
class Address(models.Model):
line1 = models.CharField(max_length=150)
line2 = models.CharField(max_length=150)
postalcode = models.CharField(max_length=10)
city = models.CharField(max_length=150)
country = models.CharField(max_length=150)
class Anniversary(models.Model):
date = models.DateField()