Josh Carvin
Josh Carvin

Reputation: 115

Django: TypeError: int() argument must be a string, a bytes-like object or a number, not

Please help! I tried searching for an answer, but I think this issue is too specific to have a generalized enough solution.

It's very difficult for me to pin point when, exactly, it is that this error started. I've Attempted too many changes now to know when the site was last working. I'm very new to this. And entirely self-taught, at that. I can assure you, it will be apparent.

when attempting to migrate I receive this error:

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

I'm going insane, and have no idea where to start! Please help!

edit: Here is the .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

edit: Here is migration 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),
        ),
    ]

Upvotes: 1

Views: 13037

Answers (2)

djvg
djvg

Reputation: 14255

This does not answer the original question, but it may help others who end up here based on the title.

One of many ways to get this error message is to use get_or_create() and forget it actually returns a tuple, as in the following example.

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

The second call to get_or_create() raises the error (at least in Django 2.2):

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Foo'

This message is not very helpful, in my opinion. Note that a similar (erroneous) call to Bar.objects.create(), instead of get_or_create(), does yield a very clear error message.

The solution in this case is simple:

foo, created = Foo.objects.get_or_create(...

Upvotes: 3

Josh Carvin
Josh Carvin

Reputation: 115

Problem solved, thanks to @MosesKoledoye.

I deleted the migrations folder inside the app that was causing the problem. And recreated it by running 'python manage.py makemigrations <appname>' I then migrated to the server, and everything was great.

Thank you, @MosesKoledoye

Upvotes: 3

Related Questions