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


i trying make author of news automatically picked when news created.

this code:

model:

from django.db import models ckeditor.fields import richtextfield django.contrib.auth.models import user  # create models here.  class news(models.model):     news_title = models.charfield(max_length=420, help_text="insert news title, specific , 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 models here.  .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) none:             obj.news_author = request.user         obj.save()  admin.site.register(news, newsadmin) 

the thing put null = true author , database fucked old news, removed , applied default on got error.. deleted older news directly phpmyadmin, no success, error still there:

valueerror: invalid literal int() base 10: 'andrei' 

there 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 try reach model page error (1054, "unknown column 'catalog_news.news_author' in 'field list'")

your model shows news_author = models.charfield, error suggests had news_author = models.foreignkey(user) @ stage instead. using foreign key better, , name author = models.foreignkey(user), since don't have repeat news model.

you've got mismatch between database , models files, , fixing can tricky. if you're in development , don't have important data in db, simplest thing delete migrations, recreate database, , run makemigrations , migrate again.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -