Jhak
Jhak

Reputation: 13

invalid literal for int() with base 10: - django - python

I was trying to make the author of a news be automatically picked when the news is created.

This is my code:

Model:

from django.db import models
from ckeditor.fields import RichTextField
from django.contrib.auth.models import User

# Create your models here.

class News(models.Model):
    news_title = models.CharField(max_length=420, help_text="Insert only the news title, be specific and short!")
    pub_date = models.DateTimeField('date published')
    article_text = RichTextField()
    news_author = models.CharField(User, max_length=250, editable = False, default='unknown')

    class Meta:
        ordering = ["news_title", "-pub_date"]
        verbose_name = "News"
        verbose_name_plural = "News"

    def get_absolute_url(self):
        return reverse('model-detail-view', args[str(self.id)])

    def __str__(self):
        return self.news_title

Admin:

from django.contrib import admin

# Register your models here.

from .models import News


admin.site.site_header = "SoLS UCP Admin Panel"

class NewsAdmin(admin.ModelAdmin):
    list_display = ('news_title', 'pub_date', 'news_author')

    def save_model(self, request, obj, form, change):
        if getattr(obj, 'news_author', None) is None:
            obj.news_author = request.user
        obj.save()

admin.site.register(News, NewsAdmin)

The thing is that I put null = True to the author and the database fucked up the old news, so I removed and applied a default on it but I got this error.. so I deleted all older news directly from phpmyadmin, but no success, the error is still there:

ValueError: invalid literal for int() with base 10: 'andrei'

There are also more before that:

  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\migrations\operations\fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\backends\base\schema.py", line 515, in alter_field
    old_db_params, new_db_params, strict)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\backends\base\schema.py", line 613, in _alter_field
    new_default = self.effective_default(new_field)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\backends\base\schema.py", line 229, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\models\fields\related.py", line 963, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\models\fields\__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\models\fields\__init__.py", line 958, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\andrei\Envs\sols-ucp\lib\site-packages\django\db\models\fields\__init__.py", line 966, in get_prep_value
    return int(value)

Also if I try to reach the model page I get this error (1054, "Unknown column 'catalog_news.news_author' in 'field list'")

Upvotes: 1

Views: 9069

Answers (2)

hassanzadeh.sd
hassanzadeh.sd

Reputation: 3461

I think you should rest your migration ,run these commands into your command line (terminal) in your project path :

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

after that migrate your project again:

python manage.py makemigrations APP_NAME
python manage.py migrate

and you attention when you migrate database, your database doesn't open in other application like database viewer.

Upvotes: 1

Alasdair
Alasdair

Reputation: 308789

Your model shows news_author = models.CharField, but the error suggests you had news_author = models.ForeignKey(User) at some stage instead. Using a foreign key is usually better, and I would name it author = models.ForeignKey(User), since you don't have to repeat news from the model.

You've now got a mismatch between the database and your models files, and fixing that can be tricky. If you're in development and don't have any important data in the db, the simplest thing would be to delete the migrations, recreate the database, and then run makemigrations and migrate again.

Upvotes: 4

Related Questions