0

I have the following in my models.py

class Department(models.Model):
    department_id = models.AutoField(primary_key=True)
    department_name = models.CharField(max_length=20,unique=True)

class Major(models.Model):
    major_id = models.AutoField(primary_key=True)
    major_name = models.CharField(max_length=20unique=True)
    department_id = models.ForeignKey(Department)

class Student(models.Model):
    ...
    department = models.ForeignKey(Department)
    major = models.ForeignKey(Major)

now in admin site,I want to create a student,when I have selected the department,how could I limit the major choices? I tried to use ModelAdmin.formfield_for_foreignkey,but I don't know how to transfer the param department.So,how to do it?

  • duplicate of this one I think: http://stackoverflow.com/questions/1824267/limit-foreign-key-choices-in-select-in-an-inline-form-in-admin – abolotnov Nov 30 '15 at 05:15
  • I had view the solutions that it gived.It's quit different from my question.What I need is that when I create a new student object in admin site,if I had selected the student department, I don't want to show all the major which is not in that department.I think there are some way to limit the major select box option – user5529959 Nov 30 '15 at 07:11
  • If your student can only choose one Major, do you need the link from Student to Department too? You can always get that going Student to Major to Department? – Ben Nov 30 '15 at 08:55
  • The link from Student to Department is to limit the choices of Major,because there are many major in the school. I want to provide a convenient way to select the major – user5529959 Nov 30 '15 at 10:38

0 Answers0