简体   繁体   English

Django内联formset通过另一个模型过滤多种关系

[英]Django inline formset filters in manytomany relationship through another model

I have two models SchoolClass and Student, which have a many-to-many relationship through an Enrolment model. 我有两个模型SchoolClass和Student,它们通过Enrollment模型有多对多的关系。

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 a field cycle (which is the year+semester that class runs in. When I view a student in the admin, I'd like to see the classes that student is enrolled in only for a given cycle (eg the current cycle) SchoolClass模型有一个字段循环(这是课程运行的年份+学期。当我在管理员中查看学生时,我希望看到学生仅在给定周期内注册的课程(例如当前周期)

I had previously had the cycle field in the Enrolment model, and the following worked nicely: 我以前在Enrollment模型中有了循环字段,以下工作很好:

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 the cycle in the the SchoolClass model, and now can't work out how to apply the filter through to the next model. 但是,我已经在SchoolClass模型中移动了循环,现在无法确定如何将过滤器应用到下一个模型。

Unless I'm overlooking something, you can do this with the queryset method on StudentEnrolmentsInline: 除非我忽略了某些内容,否则可以使用StudentEnrolmentsInline上的queryset方法执行此操作:

def queryset(self, request):
    current = Cycle.objects.latest() # or whatever to get the current cycle
    qs = super(StudentEnrolmentsInline, self).queryset(request)
    return qs.filter(school_class__cycle=current)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过中间模型具有许多内容的Django表单集 - Django formset with manytomany through intermediate model 如何通过 Django 模型进行查询并在没有 ManyToMany 关系的情况下访问另一个 Django 模型? - How to query through a Django model and get access to another Django model without a ManyToMany relationship? Django ManyToMany通过关系 - Django ManyToMany Through Relationship django_filters 通过 model 在 ManyToMany 中的字段上搜索 - django_filters search on field in ManyToMany through model Django Model 来自 ManyToMany 的表单集不接受查询集 - Django Model Formset from ManyToMany not accepting queryset Django:通过相关的 OneToOne model 使用 through 参数序列化 ManyToMany 关系 - Django: Serialize a ManyToMany relationship with a through argument via a related OneToOne model 可以在模型中限制关系的模型中限制过滤器的Django admin中的ManyToMany / Foreign Key吗? - Possible to limit filters ManyToMany/Foreign Key in Django admin for a model where the relationship is defined on the other model? 通过ManyToMany关系进行Django查询 - Django query with ManyToMany relationship through Django内联表单集不删除没有pk的中介模型? - Django Inline formset not deleting intermediary model with no pk? 如何在 Django 的内联表单集中编辑 model 对象 - How to edit model objects in an inline formset in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM