Hüseyin
Hüseyin

Reputation: 129

ValueError at / ModelForm has no model class specified

I created a Todo list app.When I try to add item to list I get an error like this; ModelForm has no model class specified.How can I solve this?

models.py

from django.db import models

class List(models.Model):
    item = models.CharField(max_length = 200)
    completed = models.BooleanField(default = False)

    def __str__(self):
        return self.item + ' | ' +  str(self.completed)

forms.py

from django import forms
from .models import List

class ListForm(forms.ModelForm):
    class Meta:
        model:List
        fields: ["item", "completed"]

views.py

from django.shortcuts import render,redirect
from .models import List
from .forms import ListForm
from django.contrib import messages

def home(request):
    if request.method == 'POST':
        form = ListForm(request.POST or None)

        if form.is_valid():
            form.save()
            all_items = List.objects.all
            messages.success(request, ('Item has been added to list'))
            return render(request, 'home.html', { 'all_items': all_items})

    else:
        all_items = List.objects.all
        return render(request,'home.html',{'all_items': all_items} )

Upvotes: 0

Views: 2431

Answers (1)

Risadinha
Risadinha

Reputation: 16666

This code as you pasted it in your question is wrong:

class ListForm(forms.ModelForm):
class Meta:
    model:List
    fields: ["item", "completed"]

It should be changed to:

class ListForm(forms.ModelForm):
    class Meta:
        model = List  # make sure to use = and not colon ":"
        fields = ["item", "completed"]  # make sure to use = and not colon ":"

BTW: List might not be the best class name, as there is also python's builtin list.

Upvotes: 3

Related Questions