我使用Django Rest Framework开发了一个API。
我刚刚更改了模型,以便通过添加 creationUser 和 updateUser 将我的对象链接到Django的User对象:
$messages = UserAdvertiserMessages::where(['user_id' => $user_id])
->orderBy('created_at', 'desc')
->skip($offset * $limit)
->take($limit)
->get()->distinct('user_to');
然后我执行:
class Document(models.Model):
name = models.CharField(max_length=100)
recipient = models.ForeignKey('Client', models.SET_NULL, null=True, verbose_name='Client')
provider = models.ForeignKey('Provider', models.SET_NULL, null=True, verbose_name='Provider')
type = models.CharField(max_length=50)
receptionDate = models.DateField()
fileName = models.CharField(max_length=200)
comment = models.TextField(blank=True, null=True)
summary = models.TextField(blank=True, null=True)
status = models.CharField(max_length=5)
creationDate = models.DateTimeField(auto_now_add=True, editable=False)
updateDate = models.DateTimeField(auto_now=True)
creationUser = models.ForeignKey(User, models.SET_NULL, null=True, related_name='creationUser') # New Line
updateUser = models.ForeignKey(User, models.SET_NULL, null=True, related_name='updateUser') # New Live
def __str__(self):
return "Id : {0} | Nom : {1} | Fournisseur : {2} | Type : {3} | Date de reception : {4}".format(self.id, self.name, self.provider, self.type, self.receptionDate)
第一行有效,但第二行提供:
pipenv run python manage.py makemigrations
pipenv run python manage.py migrate
我不明白原因。我试图删除 creationUser 和 updateUser 并再次运行这两个命令,但是问题一直在发生。
根据Bernard Parah的要求,这是 api.0027_auto_20180721_0106.py 文件:
Operations to perform:
Apply all migrations: admin, api, auth, contenttypes, inbox, sessions
Running migrations:
Applying api.0027_auto_20180721_0106...Traceback (most recent call last):
File "manage.py", line 17, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
fake_initial=fake_initial,
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\migrations\migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\backends\base\schema.py", line 407, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\backends\base\schema.py", line 152, in column_sql
default_value = self.effective_default(field)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\backends\base\schema.py", line 224, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\models\fields\related.py", line 936, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\models\fields\__init__.py", line 767, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\models\fields\__init__.py", line 939, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\mjacq\.virtualenvs\gouvernante_is_real-pYxsNaTM\lib\site-packages\django\db\models\fields\__init__.py", line 947, in get_prep_value
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'
答案 0 :(得分:0)
您的迁移文件中有问题。
field=models.ForeignKey(default=django.contrib.auth.models.User, ...
那里不应该有default
参数,并且在任何情况下User
都不是default
的适当值。我不知道会发生什么,因为default
不在您的文档模型中。尝试从迁移文件中同时删除两个default=django.contrib.auth.models.User,
,然后再次运行manage.py migrate
。
您也可以尝试删除迁移文件,然后使用makemigrations