在管理页面上为Django模型创建双向ManyToMany关系

时间:2015-06-24 22:56:25

标签: python django python-3.x django-models django-admin

我正在使用Django教程1.8& Python 3.4和现在页面上的问题可以添加多个选项,但Choices无法访问其相应的问题:

#models.py
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Published recently?'

class Choice(models.Model):
    question = models.ForeignKey('Question')
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text
#admin.py
from django.contrib import admin

from .models import Question, Choice

# Register your models here.

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

# class QuestionInline(admin.TabularInline):
#     model = Question.choice.through
#     extra = 3

class QuestionAdmin(admin.ModelAdmin):
    # fieldsets = [   (None, {'fields': ['question_text']}),
    #                 ('Date information', {'fields': ['pub_date'],'classes':
    #                 ['collapse']})
    #             ]
    list_display = ('question_text', 'pub_date', 'was_published_recently')
    list_filter = ['pub_date']
    search_fields = ['question_text']
    inlines = [ChoiceInline]

class ChoiceAdmin(admin.ModelAdmin):
    fields = ['choice_text', 'votes']

    #inlines = [QuestionInline]

admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice, ChoiceAdmin)

但是,我希望能够让问题有很多选择,并且能够在管理员中访问这些选择,然后选择也可以访问管理页面中的相应问题。

我在考虑为Choice创建一个ManyToMany变量,但这似乎删除了需要外键的TabularInline Editing的功能。

我不确定如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为我实现了我想要的部分:

models.py:

class RelatedInline(admin.TabularInline):
    model = Related
    extra = 1


class QuestionAdmin(admin.ModelAdmin):
    # fieldsets = [   (None, {'fields': ['question_text']}),
    #                 ('Date information', {'fields': ['pub_date'],'classes':
    #                 ['collapse']})
    #             ]
    #list_display = ('question_text', 'pub_date', 'was_published_recently')
    #list_filter = ['pub_date']
    #search_fields = ['question_text']
    #inlines = [ChoiceInline]
    inlines = (RelatedInline,)

class ChoiceAdmin(admin.ModelAdmin):
    #fields = ['choice_text', 'votes']
    #filter_horizontal = ('related_question',)
    #list_display = ('question',)
    #list_display_links = ('question',)
    #inlines = [QuestionInline]
    #list_filter = ('related_question', admin.RelatedOnlyFieldListFilter)
    inlines = (RelatedInline,)
    exclude = ('related_question',)
admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice, ChoiceAdmin

admin.py:

function first(y,z,fn){fn();}
function second(x,y,fn){fn();}
var btn = document.getElementsByTagName('button')[0];
btn.addEventListener('click', function () {
    first(1,2,function(){
        second(1,2,function(){ 
          //do something....
        })//exits after exec.
        alert('this never get fired');
    });
});

这样就可以为选择添加问题和问题的相关选择。

但是我也想知道在问题模型中是否有办法编辑Choice模型?或者在选择特定选择时直接链接到模型本身。

来源:https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#working-with-many-to-many-intermediary-models