本文介绍了以10为底的int()的无效文字:-django-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让新闻的作者在创建新闻时自动被选中。

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

这是我的代码:

模型:

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

管理员:

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)

问题是我将null设置为true对作者,而数据库却把旧消息搞砸了,因此我删除了它并对其应用了默认设置,但是出现了此错误..因此我直接从phpmyadmin中删除了所有较旧的新闻,但没有成功,该错误仍然存​​在:

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)

如果我尝试访问模型页面,也会收到此错误
(1054,字段列表中的未知列'catalog_news.news_author'))

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

推荐答案

您的模型显示 news_author = models.CharField ,但是该错误表明您在某个阶段使用了 news_author = models.ForeignKey(User)。使用外键通常更好,我将其命名为 author = models.ForeignKey(User),因为您不必重复 news 来自模型。

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.

您现在在数据库和模型文件之间存在不匹配,并且修复起来很棘手。如果您正在开发中,并且数据库中没有任何重要数据,那么最简单的方法是删除迁移,重新创建数据库,然后运行 makemigrations 和再次迁移

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.

这篇关于以10为底的int()的无效文字:-django-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 11:14