Sunday, February 2, 2014

hide fields in django forms

Suppose we have the following model:

models.py:

from django.db import models

# Create your models here.
class Article(models.Model):
    name= models.CharField(max_length=200)
    country= models.TextField()
    bdate = models.DateTimeField('date published')
    age= models.IntegerField(default=1, null=True, blank=True)

forms.py:

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article

The above form will show all the four (04) fields.
To hide may be the age of the student, we'll modify the forms.py as follows:

forms.py:

from django import forms
from models import Article

class ArticleForm(forms.ModelForm):

    class Meta:
        model = Article
        fields = ('name', 'country', 'bdate')

 

Done !!

No comments:

Post a Comment