0

I am facing an error that occurs whenever I try to python manage.py migrate. It says:

  Apply all migrations: admin, aufgabenzettel, auth, contenttypes, sessions
Running migrations:
  Applying aufgabenzettel.0005_auto_20210526_1527...Traceback (most recent call last):
  File "C:\Users\adam_\Documents\Coding\My_First\Aufgabenliste\manage.py", line 22, in <module>        
    main()
  File "C:\Users\adam_\Documents\Coding\My_First\Aufgabenliste\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\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 "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\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 "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
    schema_editor.add_field(
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\schema.py", line 328, in add_field
    self._remake_table(model, create_field=field)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\schema.py", line 189, in _remake_table
    self.effective_default(create_field)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\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 "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\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 "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1378, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1357, in get_prep_value
    value = super().get_prep_value(value)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1217, in get_prep_value
    return self.to_python(value)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\__init__.py", line 1318, in to_python
    parsed = parse_datetime(value)
  File "C:\Users\adam_\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\dateparse.py", line 107, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

I am quite confused and don't know what to do about it...

Here is my code:

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("<int:aufgabenzettel_id>", views.details, name="details"),
    path("add/", views.add, name="add"),
    path("delete/<int:aufgabenzettel_id>", views.delete, name="delete"),
    path("edit/<int:aufgabenzettel_id>", views.edit, name="edit"),
    path("update/<int:aufgabenzettel_id>", views.update, name="update")
]

views.py

from django.http.response import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils import timezone
from datetime import datetime

from .models import Aufgabenzettel

# Create your views here.
def index(request):
    Aufgabe_alert = Aufgabenzettel.Geplant
    now = timezone.now()
    if Aufgabe_alert == now:
        print("Timer abgelaufen!")
    Aufgaben_items = Aufgabenzettel.objects.all().order_by("-Geplant") #Aufgaben nach Datum ordnen (letzte oben durch minus)
    return render(request, "aufgabenzettel/index.html", {
        "Aufgabenliste":Aufgabenzettel.objects.all(), #für die foor loop in index wichtig GEWESEN; obsolet
        "Aufgaben_items":Aufgaben_items
    })

def details(request, aufgabenzettel_id):
    aufgabenzettel = Aufgabenzettel.objects.get(pk=aufgabenzettel_id)
    creationDate = aufgabenzettel.Datum
    dueDate = aufgabenzettel.Geplant
    return render(request, "aufgabenzettel/details.html", {
        "details":aufgabenzettel,
        "creationDate": creationDate,
        "dueDate":dueDate
    })

def add(request):
    addDatum = timezone.now()
    if request.method == "POST":
        Aufgabe = request.POST["Hinzufügen"]
        geplantes_datum = request.POST["DatumFeld"]
        Aufgabenzettel.objects.create(Aufgabeselbst=Aufgabe , Datum=addDatum, Geplant=geplantes_datum) #Aufgabenname und Aufgabendatum erstellen
        return HttpResponseRedirect(reverse("index"))
    return render(request, "aufgabenzettel/add.html")

def delete(request, aufgabenzettel_id):
    aufgabenzettel = Aufgabenzettel.objects.get(pk=aufgabenzettel_id)
    aufgabenzettel.delete()
    return HttpResponseRedirect(reverse("index"))

def edit(request, aufgabenzettel_id):
    aufgabenzettel = Aufgabenzettel.objects.get(pk=aufgabenzettel_id)
    return render(request, "aufgabenzettel/edit.html", {
        "details":aufgabenzettel
    })

def update(request, aufgabenzettel_id):
    if request.method == "POST":
        Aufgabe = Aufgabenzettel.objects.get(pk=aufgabenzettel_id)
        Aufgabe.Aufgabeselbst = request.POST["Bearbeiten"]
        Aufgabe.save()
        return HttpResponseRedirect(reverse("index"))
    return render(request, "aufgabenzettel/edit.html")

models.py

from django.db import models

# Create your models here.
class Aufgabenzettel(models.Model):
    Aufgabeselbst = models.CharField(max_length=64)
    Datum = models.DateTimeField(auto_now_add=True)
    Geplant = models.DateTimeField(auto_now_add=False, auto_now=False)

    def __str__(self):
        return f"{self.Aufgabeselbst}"

layout.html

{% load static %}
<!DOCTYPE html>
<html lang="de">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Aufgabenzettel</title>
        <link rel="stylesheet" href="{% static '/css/main.css' %}">
    </head>
    <body>
        {% block body %}
        {% endblock %}
    </body>

</html>

index.html

{% extends "aufgabenzettel/layout.html" %}

{% block body %}
<h1 id="MeineAufgaben">Meine Aufgaben</h1>
    <table>
        {% for Aufgabeselbst in Aufgaben_items %}
            <tr>
                <td>
                    <a href="{% url 'details' Aufgabeselbst.id %}"> 
                        {{ Aufgabeselbst }}
                    </a>
                </td>
                <td>
                    <form action="{% url 'delete' Aufgabeselbst.id %}" method="post">
                        {% csrf_token %}
                    <button type="submit"  id="löschenbtn">Löschen</button>
                </form>
                </td>
                <td>
                    <form action="{% url 'edit' Aufgabeselbst.id %}" method="post">
                        {% csrf_token %}
                    <button type="submit" value="{{ details }}" class="bearbeitenbtn">Bearbeiten</button>
                    </form>
                </td>
            </tr>
        {% endfor %}
    </table>
    <h2>
        <a href="{% url 'add' %}" id="neuebtn">Neue Aufgabe erstellen</a>
    </h2>
{% endblock %}

add.html

{% extends "aufgabenzettel/layout.html" %}

{% block body %}
    <h1>Füge eine neue Aufgabe hinzu</h1>
    <form action="{% url 'add' %}" method="post">
        {% csrf_token %}
        <input type="text" name="Hinzufügen" placeholder="Neue Aufgabe">
        <input type="datetime-local" name="DatumFeld">
        <button type="submit" id="Hinzufügen">Hinzufügen</button>
    </form>
{% endblock %}

details.html

{% extends "aufgabenzettel/layout.html" %}

{% block body %}
    <h1>{{ details }}</h1>
    <h3>Erstellt: {{ creationDate }}</h3>
    <h2>Geplant: {{ dueDate }}</h2>
    <a href="{% url 'index' %}">Zurück zu Aufgabe</a>
{% endblock %}

edit.html

{% extends "aufgabenzettel/layout.html" %}

{% block body %}
    <h2>Bearbeite deine Aufgabe "{{ details }}"</h2>
    <form action="{% url 'update' details.id %}" method="post">
    {% csrf_token %}
        <input type="text" name="Bearbeiten" value="{{details}}">
        <button type="submit" class="bearbeitenbtn">Bearbeiten</button>
    </form>
    <a href="{% url 'index' %}">Zurück zu Aufgabe</a>
{% endblock %}

If I run python manage.py runserver it works fine in the browser but at the same time my terminal says You have 3 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): aufgabenzettel. Run 'python manage.py migrate' to apply them. . I then executed python manage.py makemigrations and all the changes were applied. However, if I try python manage.py migrate the TypeError occurs. I don't know what to do about it and appreciate every kind of help!

4
  • problem is with date i guess Commented May 27, 2021 at 15:16
  • Have you updated any field in your models.py? You're getting this error on the migration, so I'm guessing your datetime field was a string, and you've made it a date time field. Commented May 27, 2021 at 15:17
  • Check this out: stackoverflow.com/questions/40353649/… Commented May 27, 2021 at 15:21
  • Thanks for your help guys. As mentioned in the forum stated by John Doye, I deleted my existing migrations except init.py and then executed python manage.py makemigrations and python manage.py migrate once again without any TypeError ;) Commented May 28, 2021 at 19:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.