RlM
RlM

Reputation: 277

Django migration problem with long traceback

I have a strange and frustrating problem with migration that I can't figure out. I'm not experienced in debugging. The steps before python3 manage.py makemigrations was OK. This is the log I kept. Could you please help me?

ruszakmiklos@Ruszak-MacBook-Pro firm % python3 manage.py migrate       
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, stressz
Running migrations:
  Applying stressz.0050_auto_20210504_0757...Traceback (most recent call last):
  File "/Library/Python/3.8/site-packages/django/db/models/fields/__init__.py", line 1774, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Library/Python/3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/Library/Python/3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Python/3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/Library/Python/3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Library/Python/3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "/Library/Python/3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Python/3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Library/Python/3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Library/Python/3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Python/3.8/site-packages/django/db/migrations/operations/fields.py", line 104, in database_forwards
    schema_editor.add_field(
  File "/Library/Python/3.8/site-packages/django/db/backends/sqlite3/schema.py", line 328, in add_field
    self._remake_table(model, create_field=field)
  File "/Library/Python/3.8/site-packages/django/db/backends/sqlite3/schema.py", line 189, in _remake_table
    self.effective_default(create_field)
  File "/Library/Python/3.8/site-packages/django/db/backends/base/schema.py", line 303, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/Library/Python/3.8/site-packages/django/db/models/fields/__init__.py", line 823, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/Library/Python/3.8/site-packages/django/db/models/fields/__init__.py", line 818, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Library/Python/3.8/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
    raise e.__class__(
ValueError: Field 'att_v003' expected a number but got ''.
ruszakmiklos@Ruszak-MacBook-Pro firm % 

I don't find where should I change anything with 'att_v003'

ValueError: Field 'att_v003' expected a number but got ''.

forms.py

class AttitudForm(forms.ModelForm):
class Meta:
    model = Attitud
    fields = '__all__'

    widgets = {
        'att_v001': forms.RadioSelect(attrs={'class': 'custom-radio-list'}),
        'att_v002': forms.RadioSelect(attrs={'class': 'custom-radio-list'}),
        'att_v003': forms.RadioSelect(attrs={'class': 'custom-radio-list'}),
    }

For me it seems my model is good, I can't find typerror or something else.

models.py

class Attitud(models.Model):

def __str__(self):
    return str(self.user_name)

class question(models.IntegerChoices):
    Nem_jellemző = 0
    Néha_jellemző = 1
    Nagyon_jellemző = 2

user_name = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
att_v001 = models.IntegerField(verbose_name = "a", default='', null=True, blank=False, choices=question.choices)
att_v002 = models.IntegerField(verbose_name = "b", default='', null=True, blank=False, choices=question.choices)
att_v003 = models.IntegerField(verbose_name = "c", default='', null=True, blank=False, choices=question.choices)

html

{% extends 'stressz/base.html' %}
{% block body%}
{% comment %} {% load crispy_forms_tags %} {% endcomment %}

<div class="container w-75">
<div class="display-4">Helló {{ user.get_short_name}}! 
</div>

<p class="text-justify">Kérlek, hogy az alábbi kérdések esetében válaszd ki, hogy melyek a rád leginkább jellemző kijelentések. </p>
</div>

<form method = "POST">
    <div class="container w-75 bg-light rounded-3 pt-3">
        <div class="form-group">
        {% csrf_token %}
        {{form}}

        <div class="row">
            <div class="col-4 border-right border-primary p-3">
            <h6 class="text-primary">1. Néha "felesleges" dolgokat vásárolok csupán azért, mert ez örömöt szerez nekem.</h6>
            {{ form.att_v001 }}<br>
            </div>
        
            <div class="col-4  border-primary  pt-3 pl-4">
            <h6 class="text-primary">2. Néha "felesleges" dolgokat vásárolok csupán azért, mert ez örömöt szerez nekem.</h6>
            {{ form.att_v002 }}<br>
            </div>
       

            <div class="col-4 border-left border-primary pt-3 pl-4">
            <h6 class="text-primary">3. Néha "felesleges" dolgokat vásárolok csupán azért, mert ez örömöt szerez nekem.</h6>
            {{ form.att_v003 }}<br>
            </div>

        </div>
        <button class="btn btn-large btn-primary" type="submit">Mentés</button>
`{% endblock %}`

Upvotes: 0

Views: 500

Answers (1)

Mythily Devaraj
Mythily Devaraj

Reputation: 138

In the integer column you're trying to add string value, try to remove default in models.

Instead of this

att_v003 = models.IntegerField(verbose_name = "c", default='', null=True, blank=False, choices=question.choices)

Use

att_v003 = models.IntegerField(verbose_name = "c", default=0, null=True, blank=False, choices=question.choices)

**OR**

att_v003 = models.IntegerField(verbose_name = "c", default=0, null=True, blank=False, choices=question.choices)

Upvotes: 1

Related Questions