0

Django 1.7, Python 3.0, Postgresql

Thanks for taking time to read this, I expect I am missing something obvious here.

For simplicity, let's say that my models are:

  • Student
  • AcademicClass

I am wanting to use Admin Actions to:

1st: Select Students
2nd: Create new yet-to-have-details-filled-in AcademicClass with the previously attached Students attached

Adding actions = [make_new_academic_class] and linking to that page has been fairly straight-forward, but I am completely confused as to how to attach the queryset on to that new AcademicClass.

    students = ManyToManyField('mgmt.Student', related_name='classes')

I believe I have everything correct, except this part:

def make_new_academic_class(modeladmin, request, queryset):
for stdnt in queryset:
    print(stdnt.id)  #obviously want to save this somehow
                     #then want to insert it in the AcademicClass-Student relationship
return redirect("/school/class/add")

UPDATE

Was told that the best way to do this would be to "pre-populate the form" using the Django API. Working on that.

Neal Jones
  • 450
  • 5
  • 19
  • Before creating the class object there is no way you can attach something to it, I think you can prepopulate this field via GET /school/class/?student_id=id i.e you can pass an id with redirect('/school/class/add', student_id=stdnt.id) – Esdes Mar 20 '15 at 21:15
  • @Esdes I think that this is probably the right train of thought. Thanks, I'll pursue this for a while. – Neal Jones Mar 20 '15 at 21:33

1 Answers1

0

Ok, so I figured this out:

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

I took a couple approaches on this, and this is the one that worked. Hopefully helpful to someone (or myself) in the future.

Neal Jones
  • 450
  • 5
  • 19