如何解决从 Django 端点清除和播种数据库
++更新了服务器错误的屏幕截图++
我正在尝试设置一个可以清除的 rest API,然后通过端点重新播种我的 postgres 数据库中的数据。我在 Django 中使用夹具文件中的 json 数据执行此操作,在我的视图中执行一系列函数。到目前为止,这很有效,但现在我正在尝试添加带有外键字段的数据。
models.py:
class Profile(models.Model):
name = models.CharField(max_length = 100)
profile_name = models.CharField(max_length = 100)
email = models.CharField(max_length = 100)
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length = 250)
body = models.TextField(max_length = 4000)
profile = models.ForeignKey(Profile,on_delete = models.CASCADE)
def __str__(self):
return self.title
class Comment(models.Model):
title = models.CharField(max_length = 200)
body = models.TextField(max_length = 1000)
profile = models.ForeignKey(Profile,on_delete = models.CASCADE)
post = models.ForeignKey(Post,on_delete = models.CASCADE)
def __str__(self):
return self.title
在views.py
def seed(request):
Profile.objects.all().delete()
reset(Profile)
for profile in all_profiles:
add_profile(profile)
Post.objects.all().delete()
reset(Post)
for post in all_posts:
add_post(post)
Comment.objects.all().delete()
reset(Comment)
for comment in all_comments:
add_comment(comment)
return HttpResponse('database cleared and seeded')
def add_profile(new_profile):
profile_instance = Profile.objects.create(**new_profile)
profile_instance.save()
def add_post(new_post):
post_instance = Post.objects.create(**new_post)
post_instance.save()
def add_comment(new_comment):
comment_instance = Comment.objects.create(**new_comment)
comment_instance.save()
def reset(table):
sequence_sql = connection.ops.sequence_reset_sql(no_style(),[table])
with connection.cursor() as cursor:
for sql in sequence_sql:
cursor.execute(sql)
一些示例种子对象:
all_profiles = [
{
"name": "Robert Fitzgerald Diggs","profile_name": "RZA","email": "abbotofthewu@wutang.com"
}
]
all_posts = [
{
"title": "Bring da Ruckus","body": "some text","profile": 5
}
]
all_comments = [
{
"title": "famous dart","body": "Ghostface catch the blast of a hype verse My Glock burst","profile": 6,"post": 1
}
]
现在,当我点击我的端点时,我收到类似“ValueError:无法分配“5”:“Post.profile”必须是“Profile”实例的错误。”我认为这意味着在这种情况下整数“5”只是一个数字,不被视为对任何东西的引用,但我不确定该怎么做。我认为创建模型实例会解决这个问题。
这是我在 CLI 中的错误: screenshot of server error
有什么想法吗?
解决方法
想通了。从模型创建对象只是将外键引用保存为整数,但数据库需要一个完整的实例,所以我不得不查询数据库中的外来对象,对其进行变量化并用变量覆盖整数保存前。
来自视图的新功能:
def add_profile(new_profile):
profile_instance = Profile.objects.create(**new_profile)
profile_instance.save()
def add_post(new_post):
found_profile = Profile.objects.get(id=new_post['profile'])
new_post['profile'] = found_profile
post_instance = Post.objects.create(**new_post)
post_instance.save()
def add_comment(new_comment):
found_profile = Profile.objects.get(id=new_comment['profile'])
found_post = Post.objects.get(id=new_comment['post'])
new_comment['profile'] = found_profile
new_comment['post'] = found_post
comment_instance = Comment.objects.create(**new_comment)
comment_instance.save()
,
也许尝试加载一个字符串而不是一个整数?就像在“6”而不是 6 中一样?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。