[英]Trying to add new model and field to existing data in Django
I'm learning Django and making a kind of social network and am trying to add a model and field to my models.我正在学习 Django 并制作一种社交网络,并尝试将 model 和字段添加到我的模型中。 The following post perfectly explains what I'm trying to do...
以下帖子完美地解释了我正在尝试做的事情......
Django - Private messaging conversation view Django - 私人消息对话视图
I'm trying to add the model and field after I already have some messages saved.在我已经保存了一些消息之后,我正在尝试添加 model 和字段。 I'm making this for learning purposes for now, so deleting the existing messages and starting over would probably solve the problem, but I'm trying to learn how to navigate this because next time I could have actual users and deleting the messages might not be possible.
我现在这样做是为了学习,所以删除现有消息并重新开始可能会解决问题,但我正在尝试学习如何导航,因为下次我可以拥有实际用户并且删除消息可能不会成为可能。
Here are my models:这是我的模型:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
city = models.CharField(max_length=60)
country = models.CharField(max_length=60)
skillstolearn = models.CharField(max_length=200)
skillstoteach = models.CharField(max_length=200)
description = models.TextField()
def __str__(self):
return self.user.username
class Conversation(models.Model):
participants = models.ManyToManyField(User)
def __str__(self):
return self.participants
class Message(models.Model):
sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sender')
receiver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='receiver')
content = models.TextField()
conversation = models.ForeignKey(Conversation, default=None, on_delete=models.CASCADE)
timestamp = models.DateTimeField()
def __str__(self):
return self.sender.username + ' to ' + self.receiver.username
Before trying to make the current migrations, the file did not have the Conversation model nor the conversation field in the Message model.在尝试进行当前迁移之前,该文件没有对话 model 也没有消息 model 中的对话字段。
When I try to migrate, I get:当我尝试迁移时,我得到:
django.db.utils.IntegrityError: NOT NULL constraint failed: new__thecode_message.conversation_id
How do I get this working and how do I deal with this if I need to change a model, a field, et cetera after already having users?如果我需要在已经拥有用户之后更改 model、字段等,我该如何使其工作以及如何处理?
If you add new field to model, you cannot ask to fill existing objects with None
value, when you do not accept None
values.如果您向 model 添加新字段,当您不接受
None
值时,您不能要求用None
值填充现有对象。 Add null=True
to conversation
field:将
null=True
添加到conversation
字段:
class Message(models.Model):
...
conversation = models.ForeignKey(Conversation, default=None, on_delete=models.CASCADE, null=True)
...
then makemigrations
and migrate
.然后
makemigrations
和migrate
。 It should work.它应该工作。 If not, just delete probably last migration file (with that field included) in folder
migrations
.如果没有,只需删除文件夹
migrations
中可能的最后一个迁移文件(包含该字段)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.