从 Django 端点清除和播种数据库

如何解决从 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?