Commit 55101374 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

modified the form_valid method the support adding and removing m2m major...

modified the form_valid method the support adding and removing m2m major relationships in update_student
parent b03b36c3
...@@ -227,7 +227,24 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView): ...@@ -227,7 +227,24 @@ class UpdateStudent(LoginRequiredMixin, FormValidMessageMixin, FormView):
me.room = form_room me.room = form_room
try: try:
me.major = Major.objects.get(pk=form.data['major']) # in case someone disabled the js, limit processing to only the first
# two majors passed by the user
# we also eliminate the potential a student manipulates the form to
# pass in two majors of the same type by casting to a set
form_major_pks = set(form.data.getlist('major')[:2])
# retrieve the major objects from the list of pk strings
form_majors = [Major.objects.get(pk=pk) for pk in form_major_pks]
# print(form_majors)
# iterate over a student's current majors
for current_major in me.major.all():
# remove m2m relationship if not in majors from form
if current_major not in form_majors:
me.major.remove(current_major)
# iterate over the majors in the form
for form_major in form_majors:
# add new m2m relationship to student
if form_major not in me.major.all():
me.major.add(form_major)
except: except:
me.major = None me.major = None
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment