Reputation: 7799
I have a Django model with few fields, In the django forms which is created using that django models. While using the forms.save method I also want to save the extra fields which were not present in Django forms but present in django models.
models.py
class NewProvisionalEmployeeMail(models.Model):
STATUS_CHOICES = (
(1, ("Permanent")),
(2, ("Temporary")),
(3, ("Contractor")),
(4, ("Intern"))
)
PAY_CHOICES = (
(1, ("Fixed")),
(2, ("Performance Based")),
(3, ("Not Assigned")),
)
POSITION_CHOICES = ()
for i, name in enumerate(Position.objects.values_list('position_name')):
POSITION_CHOICES += ((i, name[0]),)
email = models.EmailField(max_length=70, null=False, blank=False, unique=False)
token = models.TextField(blank=False, null=False)
offer_sent_by = models.CharField(max_length=50)
position_name = models.IntegerField(choices=POSITION_CHOICES, null=True, blank=True)
accepted = models.BooleanField(default=False)
name=models.CharField(max_length=30)
user_name=models.CharField(max_length=30)
pay = models.IntegerField(default=0)
title = models.CharField(max_length=25, null=True, blank=True)
pay_type = models.IntegerField(choices=PAY_CHOICES, default=3)
emp_type = models.IntegerField(choices=STATUS_CHOICES, default=1)
def __str__(self):
return str(self.offer_sent_by) +" to " + str(self.email)
def clean(self):
if(NewProvisionalEmployeeMail.objects.filter(email=str(self.email)).exists()):
NewProvisionalEmployeeMail.objects.filter(email=str(self.email)).delete()
def save(self, **kwargs):
self.clean()
return super(NewProvisionalEmployeeMail, self).save(**kwargs)
If you see it has following fields :
email
, token
, offer_sent_by
, position_name
, accepted
, name
, user_name
, pay
, title
, pay_type
, emp_type
.
Now I only want the following fields in my forms :
email
, position_name
, name
, user_name
, pay
, title
, pay_type
, emp_type
and not token
and offer_sent_by
whose values will be determined in my views.py using some logic.
forms.py
class NewProvisionalEmployeeMailForm(ModelForm):
class Meta:
model = NewProvisionalEmployeeMail
fields = ['email', 'position_name',
'name', 'user_name', 'pay',
'title', 'pay_type', 'emp_type',
]
According to my logic in my views.py the other fields values are generated inside the function, but since to save the model we have to use formname.save
, here it is NewProvisionalEmployeeMailForm.save()
. However this will only save the fields which were coming from my template form, how do I also add other left fields while saving using this dunction.
views.py
def sendoffer(request):
context = {}
new_emp_form = NewProvisionalEmployeeMailForm();
context['form'] = new_emp_form
hostname = request.get_host() + "/dummyoffer"
if request.method=='POST':
new_emp_form = NewProvisionalEmployeeMailForm(request.POST)
if(new_emp_form.is_valid()):
token = VALUE COMES FROM LOGIC
offer_sent_by = VALUE COMES FROM LOGIC
# I also want to save the fields token, offer_sent_by in my models using this form save method
new_emp_form.save()
return render(request, 'mainapp/offer.html',context)
As you see new_emp_form
save method will only save only those fields that are present in the form and not the fields token
and offer_sent_by
which is also part of the model. How do save the fields using form.save method?
Upvotes: 0
Views: 1861
Reputation: 5405
Saving the form returns an instance of NewProvisionalEmployeeMail
, so you can simply catch the returned object in a variable and set it's properties afterwards:
if(new_emp_form.is_valid()):
token = VALUE COMES FROM LOGIC
offer_sent_by = VALUE COMES FROM LOGIC
new_emp = new_emp_form.save(commit=False)
new_emp.token = token
new_emp.offer_sent_by = offer_sent_by
new_emp.save()
Upvotes: 2
Reputation: 314
for the first time we can change it as following
new_emp = new_emp_form.save(commit=False)
so that i wont save to the database.
Upvotes: 1