python - Django inline formset filters in manytomany relationship through another model -


i have 2 models schoolclass , student, have many-to-many relationship through enrolment model.

class student(models.model):     name = models.charfield(max_length=100)     code = models.charfield(max_length=10)  class schoolclass(models.model):     code = models.charfield(max_length=100)     cycle = models.foreignkey(cycle)     students = models.manytomanyfield(student,through='enrolment')  class enrolment(models.model):     student = models.foreignkey(student)     school_class = models.foreignkey(schoolclass) 

the schoolclass model has field cycle (which year+semester class runs in. when view student in admin, i'd see classes student enrolled in given cycle (e.g. current cycle)

i had had cycle field in enrolment model, , following worked nicely:

class studentenrolmentsinlineformset(baseinlineformset):     def get_queryset(self):         if not hasattr(self, '_queryset'):             qs = super(studentinlineformset, self).get_queryset().filter(cycle=current)             self._queryset = qs         return self._queryset  class studentenrolmentsinline(admin.tabularinline):     model = enrolment     formset = studentenrolmentsinlineformset  class studentadmin(admin.modeladmin):     form = studentform     inlines = (studentenrolmentsinline,) 

however, i've moved cycle in the schoolclass model, , can't work out how apply filter through next model.

unless i'm overlooking something, can queryset method on studentenrolmentsinline:

def queryset(self, request):     current = cycle.objects.latest() # or whatever current cycle     qs = super(studentenrolmentsinline, self).queryset(request)     return qs.filter(school_class__cycle=current) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -