Home > Software engineering >  django.core.exceptions.FieldError: Unknown field(s) in ModelForm
django.core.exceptions.FieldError: Unknown field(s) in ModelForm

Time:09-24

After adding a ModelForm and specifying the fields, I get this error when I try to makemigrations. New to django and programming in general, so probably something dumb.

django.core.exceptions.FieldError: Unknown field(s) (handling_time, holding_time, encounter_date, crate_time) specified for Encounter

Models.py:

from django.db import models
from django.conf import settings
import datetime 

class Animal(models.Model):
    Name = models.CharField(max_length=64, unique=True)
    Inactive_Date = models.DateField(null=True, default=None, blank=True)
    ANIMAL_TYPE_CHOICES = [
        ('Alligator', 'Alligator'),
        ('Alpaca', 'Alpaca'),
        ('Anteater', 'Anteater'),
        ('Armadillo', 'Armadillo'),
        ('Cat', 'Cat'),
        ('Chicken', 'Chicken'),
        ('Chinchilla', 'Chinchilla'),
        ('Donkey', 'Donkey'),
        ('Frog', 'Frog'),
        ('Goat', 'Goat'),
        ('Horse', 'Horse'),
        ('Insect', 'Insect'),
        ('Lizard', 'Lizard'),
        ('Millipede', 'Millipede'),
        ('Owl', 'Owl'),
        ('Penguin', 'Penguin'),
        ('Pig', 'Pig'),
        ('Snake', 'Snake'),
        ('Spider', 'Spider'),
        ('Tenrec', 'Tenrec'),
        ('Turtle', 'Turtle'),
    ]
    Animal_Type = models.CharField(
        max_length=32,
        choices=ANIMAL_TYPE_CHOICES,
        default='Cassowary')
    Comments = models.CharField(max_length=255)
    def __str__(self) -> str:
        return ('Animal: '   self.Name)
    def delete(self):
        self.Inactive_Date = datetime.datetime.today()
        self.save()

class Encounter(models.Model):
    encounter_date = models.DateField
    animal = models.ForeignKey(Animal,null = True, on_delete=models.SET_NULL)
    user = models.ForeignKey(settings.AUTH_USER_MODEL,null = True, on_delete=models.SET_NULL)
    handling_time = models.BigIntegerField
    crate_time = models.BigIntegerField
    holding_time = models.BigIntegerField
    comments = models.CharField(max_length=255)
    def __str__(self) -> str:
        return ('Encounter: '   self.User.username   '/'   self.Animal.Name)

Forms.py:

from django.forms import ModelForm
from encounters.models import Animal, Encounter
import datetime

class Open_Encounter_Form(ModelForm):
    class Meta:
        model = Encounter
        fields = ['encounter_date', 'user','animal','handling_time','crate_time','holding_time','comments']

Views.py:

from encounters.models import Animal, Encounter
from django.shortcuts import render
from django.contrib.auth import get_user_model
from django.views import generic
from django import forms
from django.http import HttpResponseRedirect
from .forms import Open_Encounter_Form

# Create your views here.

def index(request):

    num_animals = Animal.objects.all().count()
    User = get_user_model()
    num_users = User.objects.all().count()
    num_encounters = Encounter.objects.all().count()

    context = {
        'num_animals': num_animals,
        'num_users': num_users,
        'num_encounters': num_encounters,
    }

    return render(request, 'index.html', context = context)

def open_encounter(request):
    if request.method == 'POST':
        form = Open_Encounter_Form(request.POST)
        if form.is_valid():
            #save the data
            aRecord=form.save()
            return HttpResponseRedirect('/Encounter saved')
    else:
        form=Open_Encounter_Form()
    return render(request, 'open_encounter.html', {'form', form})

CodePudding user response:

you forgot to call/create it

class Encounter(models.Model):
    encounter_date = models.DateField() # forgot the ()
    ...
    handling_time = models.BigIntegerField()
    crate_time = models.BigIntegerField()
    holding_time = models.BigIntegerField()
    ...
  • Related