58π
If you need to do something to the data before saving, just create a function like:
def clean_nameofdata(self):
data = self.cleaned_data['nameofdata']
# do some stuff
return data
All you need is to create a function with the name **clean_***nameofdata* where nameofdata is the name of the field, so if you want to modify password field, you need:
def clean_password(self):
if you need to modify passwordrepeat
def clean_passwordrepeat(self):
So inside there, just encrypt your password and return the encrypted one.
I mean:
def clean_password(self):
data = self.cleaned_data['password']
# encrypt stuff
return data
so when you valid the form, the password would be encrypted.
22π
See the documentation for the save()
method
if request.method == 'POST':
userf = UsersModelForm(request.POST)
new_user = userf.save(commit=False)
username = userf.cleaned_data['username']
password = userf.cleaned_data['password']
passwordrepeat = userf.cleaned_data['passwordrepeat']
email = userf.cleaned_data['email']
new_user.password = new1
new_user.passwordrepeat = new2
new_user.save()
- [Django]-Case insensitive unique model fields in Django?
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Django: Equivalent of "select [column name] from [tablename]"
9π
You will have problems if you need to fill form from POST, change any form field value and render form again.
Here is solution for it:
class StudentSignUpForm(forms.Form):
step = forms.IntegerField()
def set_step(self, step):
data = self.data.copy()
data['step'] = step
self.data = data
And then:
form = StudentSignUpForm(request.POST)
if form.is_valid() and something():
form.set_step(2)
return render_to_string('form.html', {'form': form})
- [Django]-Import data from excel spreadsheet to django model
- [Django]-Is there a filter for divide for Django Template?
- [Django]-Django ModelForm for Many-to-Many fields
6π
Override _clean
methods and put your checks in them. You can modify cleaned_data
from there.
E.g:
def clean_password(self):
new1 = self.cleaned_data['password']
return new1
Every fields in the form will have a field_name_clean()
method created automatically by Django. This method is called when you do form.is_valid()
.
- [Django]-Add inline model to django admin site
- [Django]-How to run Debug server for Django project in PyCharm Community Edition?
- [Django]-Django β Annotate multiple fields from a Subquery
3π
In Views:
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.my_field = 'value'
obj.save()
In Forms:
instance = form.instance
instance.user = request.user
instance.save()
But be careful, this does not check is_valid()
. If you want to do that, you can instantiate the form with the new values:
# NOT TESTED, NOT SURE IF THIS WORKS...
form = MyForm(instance=instance)
if form.is_valid():
form.save()
- [Django]-How can i test for an empty queryset in Django?
- [Django]-LEFT JOIN Django ORM
- [Django]-Django and Middleware which uses request.user is always Anonymous
0π
The original question is "to modify/update a form field value before saving". Some methods above seem to be used after saving. This reference may be useful in solving This QueryDict instance is immutable. You may try the following method
if request.method == 'POST':
userf = UsersModelForm(request.POST)
...
data = request.POST.copy()
# remember old state
_mutable = data._mutable
# set to mutable
data._mutable = True
data['password'] = password
data['passwordrepeat'] = passwordrepeat
# set mutable flag back
data._mutable = _mutable
userf = UsersModelForm(data)
if userf.is_valid():
userf.save()
- [Django]-Django DateField default options
- [Django]-Get request data in Django form
- [Django]-Django and Middleware which uses request.user is always Anonymous