我正在尝试将一个外键添加到posts类,以便每个人都可以有多个帖子,但我不断收到外键不匹配错误。这些表是空的。我不明白这里是什么问题。
"""
Definition of models.
"""
from django.db import models
# Create your models here.
class Users(models.Model):
name = models.CharField(primary_key=True, max_length=30)
class Posts(models.Model):
priority = models.CharField(max_length=30)
client = models.CharField(max_length=30)
title = models.CharField(max_length=150)
assigned_to = models.ForeignKey(Users, on_delete=models.CASCADE)
exp_comp_time = models.FloatField(max_length=4)
percent_comp = models.FloatField(max_length=4)
post_date = models.CharField(max_length=20)
due_date = models.CharField(max_length=20)
latest_mod = models.CharField(max_length=20, null=True)
>>> newPost = Posts(priority="low", client="maserati", title="models",
assigned_to=newUser.name, exp_comp_time=5.5, percent_comp=75.2,
post_date="6/25/19", due_date="7/1/19")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
repos\DjangoWebProject1\DjangoWebProject1\office_proj_en
v\lib\site-packages\django\db\models\base.py", line 483, in __init__
_setattr(self, field.name, rel_obj)
File
repos\DjangoWebProject1\DjangoWebProject1\office_proj_en
v\lib\site-packages\django\db\models\fields\related_descriptors.py", line
211,
in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'Dave'": "Posts.assigned_to" must be a "Users"
instance.
>>> newPost = Posts(priority="low", client="maserati", title="models",
assigned_to="Dave", exp_comp_time=5.5, percent_comp=75.2,
post_date="6/25/19", due_date="7/1/19")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
repos\DjangoWebProject1\DjangoWebProject1\office_proj_en
v\lib\site-packages\django\db\models\base.py", line 483, in __init__
_setattr(self, field.name, rel_obj)
File
repos\DjangoWebProject1\DjangoWebProject1\office_proj_en
v\lib\site-packages\django\db\models\fields\related_descriptors.py", line
211, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "'Dave'": "Posts.assigned_to" must be a "Users"
instance.
当我进行迁移时,出现此错误: django.db.utils.operationalError:外键不匹配-引用“ app_users”的“ app_posts”