I am trying to make a form using modelsform, it was working fine but, suddenly I don't know what suddenly happens and it started giving me this error django.core.exceptions.FieldError: Unknown field(s) (PhoneNumber, City, Email, KYCDocument, Image, Speciality) specified for Doctor
I have checked this error online and tried some solutions but nothing workout form me .
here is forms.py file
from django import forms
from .models import Doctor
class Doctorslist(forms.ModelForm):
class Meta:
model = Doctor
fields = ('name','PhoneNumber','Email','City','Speciality','Image','KYCDocument')
here is models.py file
from django.db import models
from phonenumber_field.modelfields import PhoneNumberField
# Create your models here.
class Doctor(models.Model):
name = models.CharField(max_length=20)
phone_number = PhoneNumberField(null=False, blank=False, unique=True)
email = models.EmailField(max_length = 100)
city = models.CharField(max_length=100)
speciality = models.CharField(max_length=50)
doc_image = models.ImageField(upload_to = 'blog_images', verbose_name = "Image")
kycdocument = models.ImageField(upload_to = 'blog_images', verbose_name = "kycImage")
CodePudding user response:
The fields should take the name of the fields in your model, so:
class Doctorslist(forms.ModelForm):
class Meta:
model = Doctor
fields = ('name', 'phone_number', 'email', 'city', 'speciality', 'doc_image', 'kycocument')
You thus should not use the verbose_name
of a field, or capitalize the fields. These are simply the names of the fields you define in the form.
If you want to show these fields in a different way at the HTML side, you can define a verbose_name=…
[Django-doc] for that field, or set a label
in the form. For example:
class Doctor(models.Model):
name = models.CharField(verbose_name="Doctor's name", max_length=20)
# …
or in the form with the labels
option [Django-doc]:
class Doctorslist(forms.ModelForm):
class Meta:
model = Doctor
fields = ('name', 'phone_number', 'email', 'city', 'speciality', 'doc_image', 'kycocument')
labels = {
'name': "doctor's name"
}