2017-08-21 1 views
0

私はdjangoとやり取りしようとしていますが、これまでのところDBの繰り返しの問題以外は驚くべきものでした。DBがDjangoで絶え間なく爆発しています

私の最新の例の本を読んでDjangoに従っていると私は手紙にすべてを続いてきた、まだPythonシェル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. 

この複数のチュートリアルに続いて起こっていると私は困惑しています。私は標準的な指示に従って、ターミナル経由でpython、python、djangoをインストールしました。また、なぜこれが起こっているかわからない仮想envを使用しています。

+0

あなたのPostモデルを期待Userインスタンス私は、固定:) –

+0

ああ、それは恥ずかしいです(あなたが代わりにユーザーインスタンス変数の「ユーザー」リテラル文字列を渡している)が、それでもライン69とラインのためのトレースバックのエラーを取得しています61. – user8467470

+0

あなたの助けをありがとう - 私はちょうど本の正誤表をチェックし、var post =は宣言されていません。 – user8467470

答えて

1

文の代わりにこれを使用してください。

'user'文字列の代わりにuser変数を使用してください。

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

ありがとう。痛い初心者のエラー:) – user8467470

関連する問題