0
こんにちは、私はまだdjangoに新しいです。私はクイズ、質問と選択肢を追加することができます管理者を作成することを計画しています。クイズには質問があり、質問には選択肢があります。私は(クイズと質問)と(質問と選択)のみ行うことができます。djangoは複数の管理者リンクを登録します
ここは私のモデルです。
class Quiz(models.Model):
quiz_title = models.CharField(max_length=200)
created_date = models.DateTimeField('date created')
expired_date = models.DateTimeField(
null=True, blank=True)
def __str__(self):
return self.quiz_title
class Question(models.Model):
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
question_text = models.CharField(max_length=200)
answer = models.CharField(max_length=200,blank=True)
def __str__(self):
return self.question_text
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
def __str__(self):
return self.choice_text
class UserSession(models.Model): #for future use.
user = models.ForeignKey(User)
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
user_answer = models.CharField(max_length=200)
、ここではdjango.contribインポート管理者からの私の管理者 ある
# Register your models here.
from .models import Choice, Question, Quiz
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class QuestionInline(admin.TabularInline):
model = Question
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date Information', {'fields': ['question_text'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_filter = ['question_text']
search_fields = ['question_text']
list_display = ('question_text',)
class QuizAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['quiz_title']}),
('Date Information', {'fields': ['created_date'], 'classes': ['collapse']}),
]
inlines = [QuestionInline]
list_filter = ['created_date']
search_fields = ['quiz_title']
list_display = ('quiz_title', 'created_date')
admin.site.register(Quiz, QuizAdmin)
#~ admin.site.register(Question, QuestionAdmin)
おかげ裕二。 :D –