首页 > 解决方案 > 长追溯的 Django 迁移问题

问题描述

我对迁移有一个奇怪而令人沮丧的问题,我无法弄清楚。我没有调试经验。之前的步骤python3 manage.py makemigrations没问题。这是我保存的日志。请你帮助我好吗?

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 % 

我不知道应该在哪里用 'att_v003' 改变任何东西

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

表格.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'}),
    }

对我来说,我的模型似乎很好,我找不到 typerror 或其他东西。

模型.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 %}`

标签: pythondjangomigration

解决方案


在您尝试添加字符串值的整数列中,尝试删除模型中的默认值。

而不是这个

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

采用

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)

推荐阅读