Reputation: 86
This is my models.py
class Student(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField(max_length=50, blank=True)
ids = models.CharField(max_length=20)
def __str__(self):
return self.ids
class Teacher(models.Model):
name = models.CharField(max_length=50)
subject = models.CharField(max_length=50)
department = models.CharField(max_length=50)
def __str__(self):
return self.name
This is my forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import Student, Teacher
class Rform(UserCreationForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email', 'password1', 'password2']
class Tform(forms.ModelForm):
class Meta:
model = Teacher
fields = '__all__'
class Istudent(forms.ModelForm):
class Meta:
model = Student
fields = '__all__'
In this forms.py every class inheritance forms.ModelForm we are not declare model name in each class but every class show rightly model field in every form how?
Upvotes: 0
Views: 50
Reputation: 1215
Django provides a helper class that lets you create a Form class from a Django model. You can create a form using django.contrib.auth.forms that django gives you and u can create your form using model and if u are using model, the django gives ModelForm that relate to the Model and its feild. If You not get it what i want to describe than plzz checkout this link
https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/
Upvotes: 2