简体   繁体   English

尝试将新的 model 和字段添加到 Django 中的现有数据中

[英]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 .然后makemigrationsmigrate 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.

相关问题 Django 在管理 model 时尝试添加现有字段 - Django trying to add an existing field when making a model managed 将新的唯一字段添加到现有Django模型时的最佳实践 - Best practice when add a new unique field to an existing django model 向现有模型添加新字段-django.db.utils.ProgrammingError:列{field}不存在 - Add A New Field To Existing Model - django.db.utils.ProgrammingError: column {field} does not exist Django将字段添加到现有模型应用程序 - Django add field to existing model app 根据具有South的现有字段将新字段添加到具有值的模型中 - Add new field to model with value based on an existing field with South Django从现有模型字段条目创建新变量 - Django creating a new variable from an existing model field entry django makemigrations在尝试将ForeignKey添加到现有模型时失败 - django makemigrations Failing When Trying to Add ForeignKey to Existing Model Django 1.5:如何使用South向现有表中添加新模型字段而不必删除表? - Django 1.5: How do I add a new model field to an existing table using South without having to drop a table? Django:在数据视图中添加新字段 - Django: Add new field in Data View Django创建新模型存储现有用户及其数据的记录 - Django on creating new model store records of existing user and its data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM