Django:TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是

时间:2016-10-06 19:12:55

标签: python django typeerror

请帮忙!我试着寻找答案,但我认为这个问题太具体了,无法得到足够广泛的解决方案。

当我确切地说,这个错误开始时,我很难确定点。我现在尝试了太多的更改,以了解网站上次工作的时间。我对此很新。完全是自学成才的。我可以向你保证,这很明显。

尝试迁移时收到此错误:

when attempting to migrate I receive this error:

     Apply all migrations: admin, auth, contenttypes, purchase_log, sessions
Running migrations:
  Applying purchase_log.0009_auto_20161005_1524...Traceback (most recent call la
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    utility.execute()
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    self.execute(*args, **cmd_options)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    output = self.handle(*args, **options)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    fake_initial=fake_initial,
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_i
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    state = migration.apply(state, schema_editor)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    field,
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    self._remake_table(model, create_fields=[field])
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    self.effective_default(field)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    prepared=False)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    value = self.get_prep_value(value)
  File "C:\Users\jdcar\AppData\Local\Programs\Python\Python35-32\lib\site-packag
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not

我疯了,不知道从哪里开始!请帮忙!

编辑:这是.models.py

from django.db import models
from django.contrib.auth.models import User


class Store(models.Model):
    name = models.CharField(max_length=250)
    owner = models.ForeignKey(User)

    def __str__(self):
        return self.name


class Product(models.Model):
    type = models.CharField(max_length=250)
    owner = models.ForeignKey(User)

    def __str__(self):
        return self.type


class Receipt(models.Model):
    store = models.ForeignKey(Store)
    date = models.DateField()
    line_items = models.ManyToManyField(Product, through='ReceiptProduct')
    owner = models.ForeignKey(User)

    def __str__(self):
        return self.store.name + ': ' + str(self.date)


class ReceiptProduct(models.Model):
    receipt = models.ForeignKey(Receipt)
    product = models.ForeignKey(Product)
    price = models.FloatField()
    sale = models.BooleanField()
    description = models.CharField(max_length=500, null=True, blank=True)
    owner = models.ForeignKey(User)

    def __str__(self):
        return self.product.type

编辑:这是迁移0009_auto_20161005_1524.py

# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2016-10-05 19:24
from __future__ import unicode_literals

from django.conf import settings
import django.contrib.auth.models
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('purchase_log', '0008_receiptproduct_sale'),
    ]

    operations = [
        migrations.AddField(
            model_name='product',
            name='owner',
            field=models.ForeignKey(default=django.contrib.auth.models.User, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='receipt',
            name='owner',
            field=models.ForeignKey(default=django.contrib.auth.models.User, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='receiptproduct',
            name='owner',
            field=models.ForeignKey(default=django.contrib.auth.models.User, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='store',
            name='owner',
            field=models.ForeignKey(default=django.contrib.auth.models.User, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
        ),
    ]

2 个答案:

答案 0 :(得分:2)

问题解决了,感谢@MosesKoledoye。

我删除了导致问题的应用内的迁移文件夹。并通过运行&#39; python manage.py makemigrations <appname>&#39;重新创建它。然后我迁移到服务器,一切都很棒。

谢谢@MosesKoledoye

答案 1 :(得分:0)

这不能回答原始问题,但可能会帮助其他根据标题结尾的人。

获取此错误消息的许多方法之一是使用ref.current并忘记它实际上返回了get_or_create(),如下例所示。

tuple

第二次调用foo = Foo.objects.get_or_create(some_attribute='something') # foo is actually a tuple... Bar.objects.get_or_create(foo=foo) # this will raise the error 会引发错误(至少在Django 2.2中如此):

get_or_create()

我认为此消息不是很有帮助。请注意,对TypeError: int() argument must be a string, a bytes-like object or a number, not 'Foo' 而不是Bar.objects.create()的类似(错误)调用的确会产生非常清晰的错误消息。

这种情况下的解决方案很简单:

get_or_create()