how to remove specific form field from validating (is_valid) django - django

There are four field in django form and i am using is_valid() function to validate, but instead of validating the whole form i want to exclude one field which should not be validate using is_valid() function

You can use exclude parameter inside your form to exclude the fields you don't want to get validated.
Let's say you have a model named MyModel and a model form for it named MyForm
# forms.py
from django import forms
from .models import MyModel
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
exclude = ['field_name_you_want_to_exclude']

You can use the clean() method in your model to specify the fields you want to validate https://docs.djangoproject.com/en/2.1/ref/models/instances/#django.db.models.Model.clean

Related

django don't pass form in html in templates [duplicate]

Could anyone explain to me similarities and differences of Django's forms.Form & forms.ModelForm?
Forms created from forms.Form are manually configured by you. You're better off using these for forms that do not directly interact with models. For example a contact form, or a newsletter subscription form, where you might not necessarily be interacting with the database.
Where as a form created from forms.ModelForm will be automatically created and then can later be tweaked by you. The best examples really are from the superb documentation provided on the Django website.
forms.Form:
Documentation: Form objects
Example of a normal form created with forms.Form:
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
forms.ModelForm:
Documentation: Creating forms from models
Straight from the docs:
If your form is going to be used to
directly add or edit a Django model,
you can use a ModelForm to avoid
duplicating your model description.
Example of a model form created with forms.Modelform:
from django.forms import ModelForm
from . import models
# Create the form class.
class ArticleForm(ModelForm):
class Meta:
model = models.Article
This form automatically has all the same field types as the Article model it was created from.
The similarities are that they both generate sets of form inputs using widgets, and both validate data sent by the browser. The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database.
Here's how I'm extending the builtin UserCreationForm myapp/forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(UserCreationForm):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.CharField(max_length=75)
class Meta(UserCreationForm.Meta):
fields = ('username','first_name','last_name', 'email')
The difference is simple, ModelForm serves to create the form of a Model.
meaning that Model is designed to create kind of schema of your table where you will save data from form submission and ModelForm simply creates a form of the model (from the schema of the table)
# This creates a form from model Article
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter']
Form is a common form that is unrelated to your database (model ).
# A simple form to display Subject and Message field
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
To say in other words,
If you have a model in your app and you want to create a Form to enter data in that model (and by it to a db) use forms.ModelForm
If you simple want to create a form using django use form.Form
But you can also use this together:
from django import forms
# A simple form to display Subject and Message field
class ContactForm(forms.ModelForm):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
class Meta:
model = Contact #when you have this model
fields = [
'subject',
'message',
]

problem in form django have problem in form django i wannt someone to selve [duplicate]

Could anyone explain to me similarities and differences of Django's forms.Form & forms.ModelForm?
Forms created from forms.Form are manually configured by you. You're better off using these for forms that do not directly interact with models. For example a contact form, or a newsletter subscription form, where you might not necessarily be interacting with the database.
Where as a form created from forms.ModelForm will be automatically created and then can later be tweaked by you. The best examples really are from the superb documentation provided on the Django website.
forms.Form:
Documentation: Form objects
Example of a normal form created with forms.Form:
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
forms.ModelForm:
Documentation: Creating forms from models
Straight from the docs:
If your form is going to be used to
directly add or edit a Django model,
you can use a ModelForm to avoid
duplicating your model description.
Example of a model form created with forms.Modelform:
from django.forms import ModelForm
from . import models
# Create the form class.
class ArticleForm(ModelForm):
class Meta:
model = models.Article
This form automatically has all the same field types as the Article model it was created from.
The similarities are that they both generate sets of form inputs using widgets, and both validate data sent by the browser. The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database.
Here's how I'm extending the builtin UserCreationForm myapp/forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(UserCreationForm):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.CharField(max_length=75)
class Meta(UserCreationForm.Meta):
fields = ('username','first_name','last_name', 'email')
The difference is simple, ModelForm serves to create the form of a Model.
meaning that Model is designed to create kind of schema of your table where you will save data from form submission and ModelForm simply creates a form of the model (from the schema of the table)
# This creates a form from model Article
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter']
Form is a common form that is unrelated to your database (model ).
# A simple form to display Subject and Message field
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
To say in other words,
If you have a model in your app and you want to create a Form to enter data in that model (and by it to a db) use forms.ModelForm
If you simple want to create a form using django use form.Form
But you can also use this together:
from django import forms
# A simple form to display Subject and Message field
class ContactForm(forms.ModelForm):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
class Meta:
model = Contact #when you have this model
fields = [
'subject',
'message',
]

How to overwrite clean and clean_fieldname for ModelFormMixin

I found this
How to properly overwrite clean() method
and this
Can `clean` and `clean_fieldname` methods be used together in Django ModelForm?
but it seems to work differently if one is using the generic class mixins ModelFormMixin.
My class is also derived from ProcessFormView.
Is def form_valid(self, form): the only point where I can overwrite the form handling process?
You are confusing views and forms. A CreateView for example makes use of a ModelForm to create an object. But instead of letting the view construct the ModelForm, you can specify such form yourself and then pass this as form_class to the view.
For example say you have a Category model with a name field, and you wish to validate that the name of the Category is all written in lowercase, you can define a ModelForm to do that:
from django import forms
from django.core.exceptions import ValidationError
class CategoryForm(forms.ModelForm):
def clean_name(self):
data = self.cleaned_data['recipients']
if not data.islower():
raise ValidationError('The name of the category should be written in lowercase')
return data
class Meta:
model = Category
fields = ['name']
now we can plug in that ModelForm as form for our CategoryCreateView:
from django.views.generic import CreateView
class CategoryCreateView(CreateView):
model = Category
form_class = CategoryForm
The validation thus should be done in the ModelForm, and you can then use that form in your CreateView, UpdateView, etc.

Two model in one UpdateView "Django"

my views :
from .models import Settings, SocialMediaSetting
class UpdateSocialMediaSetting(LoginRequiredMixin, UpdateView):
model = SocialMediaSetting
fields = '__all__'
template_name = "staff/settings/social-media-settings-update.html"
class UpdateSettings(LoginRequiredMixin, UpdateView):
model = Settings
fields = '__all__'
template_name = "staff/settings/settings-update.html"
two different class in models without any relation, I want show both in one html and one form both models has just one object
you'll need to ditch the UpdateView, but this way won't take long to implement
Use a TemplateView (at least you won't have to change your other mixin)
Create two ModelForms
override the get_context_data() to pass the forms to the template
when forms are submitted, override the post() method to save them yourself
mchesler613 wrote an awesome article explaining how to do this here.
(make sure to give him a star)

Django's forms.Form vs forms.ModelForm

Could anyone explain to me similarities and differences of Django's forms.Form & forms.ModelForm?
Forms created from forms.Form are manually configured by you. You're better off using these for forms that do not directly interact with models. For example a contact form, or a newsletter subscription form, where you might not necessarily be interacting with the database.
Where as a form created from forms.ModelForm will be automatically created and then can later be tweaked by you. The best examples really are from the superb documentation provided on the Django website.
forms.Form:
Documentation: Form objects
Example of a normal form created with forms.Form:
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
forms.ModelForm:
Documentation: Creating forms from models
Straight from the docs:
If your form is going to be used to
directly add or edit a Django model,
you can use a ModelForm to avoid
duplicating your model description.
Example of a model form created with forms.Modelform:
from django.forms import ModelForm
from . import models
# Create the form class.
class ArticleForm(ModelForm):
class Meta:
model = models.Article
This form automatically has all the same field types as the Article model it was created from.
The similarities are that they both generate sets of form inputs using widgets, and both validate data sent by the browser. The differences are that ModelForm gets its field definition from a specified model class, and also has methods that deal with saving of the underlying model to the database.
Here's how I'm extending the builtin UserCreationForm myapp/forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(UserCreationForm):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
email = forms.CharField(max_length=75)
class Meta(UserCreationForm.Meta):
fields = ('username','first_name','last_name', 'email')
The difference is simple, ModelForm serves to create the form of a Model.
meaning that Model is designed to create kind of schema of your table where you will save data from form submission and ModelForm simply creates a form of the model (from the schema of the table)
# This creates a form from model Article
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter']
Form is a common form that is unrelated to your database (model ).
# A simple form to display Subject and Message field
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
To say in other words,
If you have a model in your app and you want to create a Form to enter data in that model (and by it to a db) use forms.ModelForm
If you simple want to create a form using django use form.Form
But you can also use this together:
from django import forms
# A simple form to display Subject and Message field
class ContactForm(forms.ModelForm):
subject = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
class Meta:
model = Contact #when you have this model
fields = [
'subject',
'message',
]