sherpaurgen
sherpaurgen

Reputation: 3274

Django: what do model and fields do in meta class?

I am following the Django tutorial series from "trydjango: coding for enterpreneurs", and was confused about what "model" and "field" do in a Django form.

models.py

from django.db import models
# Create your models here.
class SignUp(models.Model):
    email=models.EmailField()
    full_name=models.CharField(max_length=55, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated=models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.email

forms.py

from django import forms
from .models import SignUp

class SignUpForm(forms.ModelForm):
    class Meta:
        model=SignUp   # ?
        fields=['full_name','email']  # ?

    def clean_email(self):
        email=self.cleaned_data.get('email')
        email_base,provider=email.split("@")
        domain,extension=provider.split(".")
        if not domain == 'USC':
            raise forms.ValidationError("Please make sure you use your USC email")
        if not extension == "edu":
            raise forms.ValidationError("Please use valide edu address")
        return email

    def clean_full_name(self):
        full_name = self.cleaned_data.get('full_name')
        #write validation code
        return full_name

Upvotes: 3

Views: 7658

Answers (1)

R Bazhenov
R Bazhenov

Reputation: 178

You are using a special Form class which allows you to create a new Form auto-magically from the specified Model class. For more refer to the documentation.

Model field shows which Model your Form would be created from and Fields field shows which fields from the Model class to show in your new Form.

Upvotes: 8

Related Questions