简体   繁体   English

在Django管理页面中过滤ManyToManyField

[英]Filter a ManyToManyField in django admin page

The problem 问题

I have two many to many fields ( disciplines , subjects ) showing in Excerpt Model and I would like to show in admin page only the subjects inside disciplines that corresponde. 我有两个多对多的领域( disciplinessubjects )中显示Excerpt模式,我想在管理页面只显示subjects里面disciplines是corresponde。

Example

DISCIPLINE | SUBJECT
portugues  | article
portugues  | verbs
portugues  | nons
math       | numbers
math       | equations

DISCIPLINE = math

DISCIPLINE | SUBJECT
math       | numbers
math       | equations

How I partially solved 我如何部分解决

For that I'm using the function formfield_for_manytomany inside admin, it works well for filtering the objects, but I couldn't figure out how to filter the subject objects based on the discipline choosed, 为此,我在admin内部使用了formfield_for_manytomany函数,它可以很好地过滤对象,但是我不知道如何根据formfield_for_manytomany discipline来过滤subject对象,

What I Tried 我尝试过的

I tried use a function in model.py get_disciplines for return the corresponding id, for then filtering the subjects, but this approach doesn't seem to work inside my admin proxy model, because raise an Error global name 'get_disciplines' is not defined probably cuz I can access outside model.py. 我尝试在model.py get_disciplines使用一个函数来返回相应的id,然后过滤主题,但是这种方法似乎在我的管理代理模型中不起作用,因为引发错误的global name 'get_disciplines' is not defined可能global name 'get_disciplines' is not defined因为我可以在model.py之外访问。 I tried also declare this method inside admin proxy, but also doesnt work. 我试过也可以在管理代理中声明此方法,但是也行不通。

my code 我的代码

# model.py
class Discipline(models.Model):
    id = models.CharField(max_length=15, primary_key=True)
    name = models.CharField(max_length=100)

class Subject(models.Model):
    id = models.CharField(max_length=15, primary_key=True)
    name = models.CharField(max_length=100)
    disciplines = models.ManyToManyField(Discipline)

class Excerpt(models.Model):
    discipline = models.ManyToManyField(Discipline)
    subjects = models.ManyToManyField(Subject)

    # my idea
    def get_disciplines(self):
        e = self.discipline.first()
        return e.id

# admin.py
class ExcerptTaggerAdmin(ImageCroppingMixin, admin.ModelAdmin):
    filter_horizontal = ('subjects','discipline')

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "subjects":
            kwargs["queryset"] = Subject.objects.filter(disciplines = get_discipline)
        return super(ExcerptTaggerAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

class ExcerptTagger(Excerpt):
    class Meta:
        proxy = True

I'm not a pro in django, so I think can be simple for someone that has more experience, I hope you can help me 我不是django的专业人士,所以我认为对于有更多经验的人来说很简单,希望您能为我提供帮助

I found a solution but its not so nice, but its working 我找到了一个解决方案,但不是很好,但是可以正常工作

def formfield_for_manytomany(self, db_field, request, **kwargs):
    if db_field.name == "subjects":
        self_pub_id = request.resolver_match.args[0]

        d = Excerpt.objects.get(id=self_pub_id).discipline.first()

        kwargs["queryset"] = Subject.objects.filter(disciplines=d)

    return super(ExcerptTaggerAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

If someone has a better solution, I would appreciate 如果有人有更好的解决方案,我将不胜感激

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM