简体   繁体   English

TypeError:int()参数必须是字符串或数字,而不是'Model Instance'-Heroku

[英]TypeError: int() argument must be a string or a number, not 'Model Instance' - Heroku

Trying to deploy my project on the server, and i'm stuck in migrations becouse there is some error: 尝试在服务器上部署我的项目,由于出现一些错误,我陷入了迁移困境:

  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 382, in add_field
definition, params = self.column_sql(model, field, include_default=True)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 145, in column_sql
default_value = self.effective_default(field)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/related.py", line 915, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
value = self.get_prep_value(value)
File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'Profile'

And here is my models.py file: 这是我的models.py文件:

class Post(models.Model):
author = models.ForeignKey('auth.User')
title = models.CharField(max_length=75)
image = models.ImageField(null=True, blank=True, width_field="width_field", height_field="height_field", upload_to='images') #must be installed Pillow for ImageField
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
content = models.TextField()
updated = models.DateTimeField(default=datetime.now())
timestamp = models.DateTimeField(default=datetime.now())


def __str__(self):
    return self.title

@receiver(pre_delete, sender=Post)
def post_delete(sender, instance, **kwargs):
# Pass false so FileField doesn't save the model.
if instance.image:
    instance.image.delete(False)




class Profile(models.Model):
onlyletters = RegexValidator(r'^[a-zA-Z]*$', 'Only letters are allowed.')
author = models.ForeignKey('auth.User')
name = models.CharField(max_length=20, null = True, validators=[onlyletters])
surname = models.CharField(max_length=25, null = True, validators=[onlyletters])
city = models.CharField(max_length=30, blank=True, validators=[onlyletters])
birth_date = models.DateField(blank=True, null=True, help_text="Can not be more than 100 years - (Format yyyy-mm-dd)")

topic = models.CharField(max_length = 50, null=True)


def __str__(self):
    return self.topic

class Favourite(models.Model):
name = models.CharField(max_length=50)
members = models.ManyToManyField(Profile, through='Membership')

def __str__(self):              # __unicode__ on Python 2
    return self.name


class Membership(models.Model):
author = models.CharField(max_length=50)
profile = models.ForeignKey(Profile, default=Profile)
favourite = models.ForeignKey(Favourite)
created = models.DateTimeField(default=timezone.now)



class Comment(models.Model):
post = models.ForeignKey('blogapp.Post', related_name='comments')
author = models.ForeignKey('auth.User')
text = models.TextField()
created_date = models.DateTimeField(default=timezone.now)

def __str__(self):
    return self.text

Just trying to make some fresh app on heroku, making new migrations and deleting olders but it's still didn't work. 只是尝试在heroku上制作一些新的应用程序,进行新的迁移并删除较旧的应用程序,但仍然没有用。 Can anybody know what's wrong with it? 有人知道这是怎么回事吗? Thanks for the answer. 感谢您的回答。

Here's the culprit: 这是罪魁祸首:

profile = models.ForeignKey(Profile, default=Profile)
#                                    ^^^^^^^^^^^^^^^

You can't set a Model class as a Foreignkey default. 您不能将Model类设置为外Foreignkey默认值。 If you're thinking of setting an hardcoded default then you should use an int and be sure the selected value exists as a key in your Profile model. 如果您想设置一个硬编码默认值,则应使用一个int并确保所选值作为Profile模型中的键存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:int()参数必须是字符串或数字,而不是“模型实例” - TypeError: int() argument must be a string or a number, not 'Model Instance' TypeError:int()参数必须是字符串或数字,而不是'tuple' - TypeError: int() argument must be a string or a number, not 'tuple' 类型错误:INT()参数必须是字符串或数字,而不是“字典” - TypeError: int() argument must be a string or a number, not 'dict' TypeError:int()参数必须是字符串或数字,而不是'Binary' - TypeError: int() argument must be a string or a number, not 'Binary' TypeError:int()参数必须是字符串或数字,而不是“列表” - TypeError: int() argument must be a string or a number, not 'list' Django TypeError int()参数必须是字符串或数字,而不是'QueryDict' - Django TypeError int() argument must be a string or a number, not 'QueryDict' Hyperopt参数空间:TypeError:int()参数必须是字符串或数字,而不是“ Apply” - Hyperopt parameter space: TypeError: int() argument must be a string or a number, not 'Apply' Django错误:TypeError:int()参数必须是字符串或数字,而不是“ BuildsTable” - Django Error: TypeError: int() argument must be a string or a number, not 'BuildsTable' Django-TypeError:int()参数必须是字符串或数字,而不是'dict' - Django - TypeError: int() argument must be a string or a number, not 'dict' TypeError:int()参数必须是字符串或数字,而不是使用ouchdb-python的“元组” - TypeError: int() argument must be a string or a number, not 'tuple' with couchdb-python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM