【问题标题】:clearing and seeding database from endpoint in django从 django 的端点清除和播种数据库
【发布时间】:2021-05-04 13:30:06
【问题描述】:

++更新了服务器错误的屏幕截图++

我正在尝试设置一个可以清除的 rest API,然后通过端点重新播种我的 postgres db 中的数据。我正在使用带有 json 数据的 Django 执行此操作,并在我的视图中执行一系列函数。到目前为止效果很好,但现在我正在尝试使用外键字段添加数据。

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

有什么想法吗?

【问题讨论】:

    标签: django postgresql django-models seeding


    【解决方案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()
    

    【讨论】:

      【解决方案2】:

      也许尝试加载字符串而不是整数?就像“6”而不是 6?

      【讨论】:

        猜你喜欢
        • 2011-04-29
        • 1970-01-01
        • 2011-08-29
        • 2015-09-25
        • 2012-06-04
        • 1970-01-01
        • 2020-04-04
        • 2017-11-01
        相关资源
        最近更新 更多