动态Django模型控制

动态Django模型控制,django,Django,我正在使用django设计一个测验引擎。 在models.py中,我有这样的类: class Quiz (models.Model): quiz_id = models.AutoField (primary_key=True) problem_desc = models.TextField (blank=True) problem_has_resource = models.BoolField () problem_is_choice = models.Boole

我正在使用django设计一个测验引擎。 在models.py中,我有这样的类:

class Quiz (models.Model):
    quiz_id = models.AutoField (primary_key=True)
    problem_desc = models.TextField (blank=True)
    problem_has_resource = models.BoolField ()
    problem_is_choice = models.BooleanField ()
    def __unicode__ (self):
        return self.quiz_id

class Choice (models.Model):
    choice_id = models.AutoField (primary_key=True)
    quiz_id = models.ForeignKey (Quiz);
    choice_desc = models.CharField (max_length = 500)
    is_answer = models.BooleanField ()

class Answer (models.Model):
    quiz_id = models.ForeignKey (Quiz)
    input_answer = models.FloatField ()

class Quiz_Resource (models.Model):
    quiz_id = models.ForeignKey (Quiz)
    title = form.CharField (max_length = 50)
    file = forms.FileField ()
    def __unicode__ (self):
        return self.file.name
测验可能需要输入“答案”或选择选项。测验可能有很多选择。测验可能有一些额外的资源。我想让boolfield控制管理页面样式,设置正式信息。我怎样才能做到呢

鞠躬,谢谢


在此处输入代码首先,您必须将您的测验模型与您的选择、答案和测验源模型相关联

class Quiz (models.Model):
    name = models.TextField(max_length=50)
    problem_desc = models.TextField (blank=True)
    problem_has_resource = models.BoolField ()
    problem_is_choice = models.BooleanField ()

    choices=models.ManyToManyField(Choice)
    answers=models.ManyToManyField(Answer)
    ressources=models.ManyToManyField(Quiz_Ressource)
这样,您就可以告诉Django您的测验模型引用了类型选择、答案和测验资源的多个模型。如果不清楚,可以在关系数据库中搜索manytomy的概念

在您像那样重写模型之后,在管理页面上将有用于添加和选择选项、anwers和Resources的字段

要继续使用django,请使用此处提供的文档:

您对自定义管理模板的需求与此不同。您可以在apps admin.py中使用类似的内容来实现这一点:

from django.contrib import admin
from django.contrib.admin.sites import AdminSite
from yourapp.models import Quiz

class QuizAdminSite(AdminSite):

    def admin_quiz(request):
        #here goes your custom admin view code, where you can do
        #if has_ressource: return render_to_response('admin/quiz/has_ressource.html')
        #elif is_choice: return render_to_response('admin/quiz/is_choice.html')
        #and so on, you have to work out yourself how this has to look exactly
        return HttpResponse('your custom admin view')

    def get_urls(self):
    from django.conf.urls.defaults import *
    urls = super(ItemAdmin, self).get_urls()
    my_urls = patterns('',
        url(
            r'update_feeds',
            self.admin_site.admin_view(self.admin_quiz),
            name='admin_quiz',
        ),
    )
    return my_urls + urls    


class QuizAdmin(QuizAdminSite):
    model=Quiz

admin.site.register(Quiz,QuizAdmin)

我不知道你想用你的自定义管理员视图做什么,所以我跳过了这一部分。但这应该是我们要走的路。

谢谢!现在我想设计一个特殊的网页,作为一个管理页面工作