从django中的端点清除和设定数据库种子
++更新了服务器错误的屏幕截图++ 我正在尝试设置一个RESTAPI,该API可以被清除,然后通过端点对postgres db中的数据进行重新播种。我使用Django在fixtures文件中使用json数据执行此操作,在视图中执行一系列函数。到目前为止,这非常有效,但现在我正在尝试添加带有外键字段的数据 型号。py:从django中的端点清除和设定数据库种子,django,postgresql,django-models,seeding,Django,Postgresql,Django Models,Seeding,++更新了服务器错误的屏幕截图++ 我正在尝试设置一个RESTAPI,该API可以被清除,然后通过端点对postgres db中的数据进行重新播种。我使用Django在fixtures文件中使用json数据执行此操作,在视图中执行一系列函数。到目前为止,这非常有效,但现在我正在尝试添加带有外键字段的数据 型号。py: class Profile(models.Model): name = models.CharField(max_length = 100) profile_nam
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
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
}
]
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()
在视图中.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)
一些种子对象示例:
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
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
}
]
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()
现在,当我点击我的端点时,我得到了一个类似“ValueError:Cannot assign”5的错误:“Post.profile”必须是一个“profile”实例。我假设这意味着在这种情况下整数“5”只是一个数字,不被视为任何引用,但我不确定该怎么办。我认为创建模型实例可以解决这个问题
以下是我的CLI错误:
有什么想法吗?也许可以尝试加载字符串而不是整数?正如在“6”而不是“6”中一样?计算出了它。从模型创建一个对象只需将外键引用保存为整数,但数据库需要一个完整实例,因此我必须在数据库中查询有问题的外部对象,对其进行变量化,并在保存前用变量重写整数 视图中的新功能:
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
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
}
]
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()
谢谢,没有同样的错误。