Answers for "ModelForm"

5

django model form

class YourForm(ModelForm):
	class Meta:
    	model = YourModel
    	fields = ['pub_date', 'headline', 'content', 'reporter']
Posted by: Guest on October-07-2020
0

ModelForm

>>> from django.forms import ModelForm
>>> from myapp.models import Article

# Create the form class.
>>> class ArticleForm(ModelForm):
...     class Meta:
...         model = Article
...         fields = ['pub_date', 'headline', 'content', 'reporter']

# Creating a form to add an article.
>>> form = ArticleForm()

# Creating a form to change an existing article.
>>> article = Article.objects.get(pk=1)
>>> form = ArticleForm(instance=article)
Posted by: Guest on August-15-2021

Python Answers by Framework

Browse Popular Code Answers by Language