python - Django Admin Actions: Add "Students" to "Academic Class" -


django 1.7, python 3.0, postgresql

thanks taking time read this, expect missing obvious here.

for simplicity, let's models are:

  • student
  • academicclass

i wanting use admin actions to:

1st: select students
2nd: create new yet-to-have-details-filled-in academicclass attached students attached

adding actions = [make_new_academic_class] , linking page has been straight-forward, confused how attach queryset on new academicclass.

    students = manytomanyfield('mgmt.student', related_name='classes') 

i believe have correct, except part:

def make_new_academic_class(modeladmin, request, queryset): stdnt in queryset:     print(stdnt.id)  #obviously want save somehow                      #then want insert in academicclass-student relationship return redirect("/school/class/add") 

update

was told best way "pre-populate form" using django api. working on that.

ok, figured out:

def make_class(modeladmin, request, queryset):     new_class = class()     new_class.save()     student in queryset:         new_class.students.add(student.id)     return redirect(reverse('admin:mgmt_class_change', args=(new_class.id,))) 

i took couple approaches on this, , 1 worked. helpful (or myself) in future.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -