0
問題
一個ManyToManyField我有兩個多對多字段(disciplines
,subjects
)顯示Excerpt
模式,我想在管理頁面只顯示subjects
內disciplines
該corresponde。過濾器在Django管理頁面
例
DISCIPLINE | SUBJECT
portugues | article
portugues | verbs
portugues | nons
math | numbers
math | equations
DISCIPLINE = math
DISCIPLINE | SUBJECT
math | numbers
math | equations
我如何可以部分地解決
對於我使用的功能formfield_for_manytomany
內部管理,它可以很好地用於過濾的對象,但我無法弄清楚如何篩選根據選用的discipline
的subject
對象,
我試過
我嘗試使用本功能離子in model.py get_disciplines
返回相應的id,然後過濾主題,但這種方法似乎並不適用於我的管理代理模型,因爲引發錯誤global name 'get_disciplines' is not defined
可能因爲我可以訪問model.py之外。我也試過在管理代理中聲明這個方法,但也不起作用。
我的代碼
# 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
我不是一個職業在Django,所以我覺得可以很簡單的人,有更多的經驗,我希望你能幫助我