DB在Django中持续爆炸

2024-04-26 07:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着去和django,到目前为止,我发现这是惊人的其他重复db的问题。你知道吗

我最新的一本是遵循Django by Example的书,我已经完全遵循了这本书的所有内容,但是当我按照一些简单的说明通过python shell api添加一些数据时,我得到了以下结果:

>>> from django.contrib.auth.models import User
>>> from blog.models import Post
>>> user = User.objects.get(username='jamie')
>>> Post.objects.create(title='One More Post', slug='one-more-post', body='Post body', author='user')
Traceback (most recent call last):
  File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/core/management/commands/shell.py", line 69, in handle
self.run_shell(shell=options['interface'])
  File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/core/management/commands/shell.py", line 61, in run_shell
raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/query.py", line 346, in create
obj = self.model(**kwargs)
  File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/base.py", line 468, in __init__
setattr(self, field.name, rel_obj)
  File "/Users/jamie/dev/venv/lib/python3.6/site-packages/django/db/models/fields/related.py", line 629, in __set__
self.field.rel.to._meta.object_name,
ValueError: Cannot assign "'user'": "Post.author" must be a "User" instance.

这是发生在遵循多个教程,我被难住了。我遵循标准说明,通过终端安装了pip、python和django。也使用虚拟环境,所以不知道为什么会发生这种情况。你知道吗


Tags: djangoinpydevdbvenvmodelslib
1条回答
网友
1楼 · 发布于 2024-04-26 07:20:12

用这个代替你的陈述。你知道吗

使用user变量而不是'user'字符串。你知道吗

Post.objects.create(title='One More Post', slug='one-more-post',
                    body='Post body', author=user)

相关问题 更多 >