How can i use Django smart select to filter ManyToManyField?
class Course(models.Model):
开发者_JS百科 course_code = models.CharField(max_length=100,unique=True)
title = models.CharField(max_length=200)
short = models.CharField(max_length=50)
elective_group = models.CharField(max_length=100)
class Unit(models.Model):
title = models.CharField(max_length=100)
short = models.CharField(max_length=50)
course = models.ForeignKey(Course)
class Pattern(models.Model):
pattern_name = models.CharField(max_length=200)
class ExamSchedule(models.Model):
exam_date = models.DateTimeField()
course = models.ForeignKey(Course)
pattern = models.ForeignKey(Pattern)
**units = models.ManyToManyField(Units)**
I need to implement functionality like if user creates an Examschedule object , after selecting a course from dropdown the unit widget should only contains those units that are related to the course selected.
Thanks Anks
I've done something similar with Javascript.
Start with the Unit HTML element disabled. Once the user selects the course, an Ajax request is done to the server with the selected course and the valid units are returns. Still using Javascript you add those units to the appropriate element and only then enable it. If the user alters the Course a new request is done to update the Unit element with the appropriate units.
To speed up things, you can provide all the units to the client and also load them dynamically with Javascript once the Course is chosen. This approach reduces waiting time but increases the transfered file size (if they are not hundreds it's probably better).
精彩评论