Django Migrate:TypeError:int()参数必须是字符串或数字,而不是'User'

时间:2021-11-01 21:21:07

I have applied some changes to the model.py and when I applied makemigrations it worked fine. But after that, the migrate command gives the following error.

我已经对model.py进行了一些更改,当我应用makemigrations时,它运行正常。但之后,migrate命令会出现以下错误。

TypeError: int() argument must be a string or a number, not 'User' Here is the Traceback:

TypeError:int()参数必须是字符串或数字,而不是'User'这是Traceback:

Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, mess, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
Rendering model states... DONE
Applying mess.0003_auto_20150821_1912...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1956, in get_db_prep_save
return self.related_field.get_db_prep_save(value, connection=connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 977, in get_db_prep_value
value = self.get_prep_value(value)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'User'

Here is my models.py:

这是我的models.py:

from django.db import models
from django import forms
from django.contrib.auth.models import User as auth_user 
class Student(models.Model):
    user = models.OneToOneField(auth_user) #rollno
    rollno = models.IntegerField()
    password = models.CharField(max_length=50)
    fullname = models.CharField(max_length=200)
    email = models.EmailField()


class Banking(models.Model):

    student = models.OneToOneField(Student)
    breakfastcount = models.IntegerField(default=0)
    lunchcount  = models.IntegerField(default=0)
    dinnercount = models.IntegerField(default=0)

class Meal(models.Model):
    mealname=models.CharField(max_length=20)
    mealcost=models.IntegerField(default=0)
    MEALS = (('BREAKFAST','Break Fast'),
             ('LUNCH','Lunch'),
             ('DINNER','Dinner'))
class Booking(models.Model):
     student=models.OneToOneField(Student)
     date = models.DateField( editable=True )
     meal=models.CharField(max_length = 20, choices=MEALS)

Can anybody help me with this TypeError ?

有人可以帮我解决这个TypeError吗?

Thanks in advance.

提前致谢。

2 个解决方案

#1


1  

Solved the issue. This has been a bug in code.Django under ticket #23454 https://code.djangoproject.com/ticket/23454

解决了这个问题。这是code.Django中的一个错误,票号为#23454 https://code.djangoproject.com/ticket/23454

But this bug has been closed due to insufficient information.

但由于信息不足,此错误已被关闭。

I created a new app and replaced the files with the older one and makemigrations as well as migrate ran successfully.

我创建了一个新的应用程序,并用较旧的应用程序和makemigrations替换了文件,并成功运行了迁移。

I think this issue arises if we try to change the Django Auth system after first migrate.

我认为如果我们在首次迁移后尝试更改Django Auth系统,就会出现此问题。

Thanks

谢谢

#2


0  

What this means ?

这意味着什么?

Your function get_prep_value is returning this

你的函数get_prep_value正在返回这个

return int(value) -> Type of value is User object

But it should be of type int or string(only if its a valid integer enclosed in double quotes like "1" else an exception would be raised)

但它应该是int或string类型(只有当它包含在双引号中的有效整数,如“1”时,否则会引发异常)

Since you have not shared that function, i would suggest the following things

由于您尚未共享该功能,我建议以下内容

  1. Check what you are passing into the value.
  2. 检查您传递给值的内容。
  3. Use type (value) before your return statement so you know what you are having inside the value variable.
  4. 在return语句之前使用type(value),以便知道在value变量中有什么。

Read here about the int() which might be confusing you here

在这里阅读有关int()的信息,这可能会使您感到困惑

python int( ) function

python int()函数

#1


1  

Solved the issue. This has been a bug in code.Django under ticket #23454 https://code.djangoproject.com/ticket/23454

解决了这个问题。这是code.Django中的一个错误,票号为#23454 https://code.djangoproject.com/ticket/23454

But this bug has been closed due to insufficient information.

但由于信息不足,此错误已被关闭。

I created a new app and replaced the files with the older one and makemigrations as well as migrate ran successfully.

我创建了一个新的应用程序,并用较旧的应用程序和makemigrations替换了文件,并成功运行了迁移。

I think this issue arises if we try to change the Django Auth system after first migrate.

我认为如果我们在首次迁移后尝试更改Django Auth系统,就会出现此问题。

Thanks

谢谢

#2


0  

What this means ?

这意味着什么?

Your function get_prep_value is returning this

你的函数get_prep_value正在返回这个

return int(value) -> Type of value is User object

But it should be of type int or string(only if its a valid integer enclosed in double quotes like "1" else an exception would be raised)

但它应该是int或string类型(只有当它包含在双引号中的有效整数,如“1”时,否则会引发异常)

Since you have not shared that function, i would suggest the following things

由于您尚未共享该功能,我建议以下内容

  1. Check what you are passing into the value.
  2. 检查您传递给值的内容。
  3. Use type (value) before your return statement so you know what you are having inside the value variable.
  4. 在return语句之前使用type(value),以便知道在value变量中有什么。

Read here about the int() which might be confusing you here

在这里阅读有关int()的信息,这可能会使您感到困惑

python int( ) function

python int()函数