Set Field To String On POST Request

Ray Zuchowski
Ray Zuchowski used Ask the Experts™
on
After hitting submit button on my forum in django, i want to change the value of my field after the post request . The field is status, basically i want to pass a string into the field that says " In Session". Here is my views.py .


@login_required
def K8_Points(request):
   if request.method == 'POST':
      form = K8PointsForm(request.POST)
      if form.is_valid():
         status = "In Session"
         form.save()
           
   else:
     form = K8PointsForm()   
     return render(request, 'points/k8_points.html', {'form': form})     

Open in new window



Models.Py


class K8Points(models.Model):
    dayid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
    date = models.DateField(default=timezone.now) 
    teacher_class= models.ForeignKey(TeacherClass, on_delete = models.PROTECT, default = "")
    week_of = models.IntegerField(default=weeknumber)
    day = models.CharField(max_length= 10)
    status = models.CharField(max_length= 10, default = "", blank = True)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Assistant
Commented:
Ray
 
Once you've checked that the form is valid you can access it's cleaned_data attribute.

That's probably where you want to set the value of the status field.

Something like this perhaps.
      if form.is_valid():
         form.cleaned_data['status'] = "In Session"
         form.save()

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial