Stack Overflow上的一些人已经讨论了这个问题,但是没有答案对我有帮助。
这是post_2
post_2 = Post(title = 'Dark Knight', content='Batman was amazing in the Dark Knight', author_id=User.id)
这是models.py的内容
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
当我这样做
>>> post_2.save()
我收到错误
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'
这是完整的错误
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\base.py", line 779, in save_base
force_update, using, update_fields,
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\base.py", line 908, in _do_insert
using=using, raw=raw)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\sql\compiler.py", line 1334, in execute_sql
for sql, params in self.as_sql():
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\sql\compiler.py", line 1278, in as_sql
for obj in self.query.objs
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\sql\compiler.py", line 1278, in <listcomp>
for obj in self.query.objs
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\sql\compiler.py", line 1277, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\sql\compiler.py", line 1218, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\fields\related.py", line 937, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\fields\__init__.py", line 789, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\fields\__init__.py", line 959, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\HP\project2_env\lib\site-packages\django\db\models\fields\__init__.py", line 968, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DeferredAttribute'
答案 0 :(得分:1)
您需要一个User
实例。
author_id=User.id
对id
类的User
字段的要求。您需要更多类似的东西:
author = User(...)
post_2 = Post(title='Dark Knight', content='Batman was amazing in the Dark Knight', author=author)
post_2.save()
答案 1 :(得分:0)
谢谢大家的帮助,当我为.first()
赋值时,我并没有放user
user = User.objects.filter(username='Batman').first()