hi im still new django. im planning create admin can add quiz,question , choice. quiz has question , question has choice. can (quiz , question) , (question , choice).
here model.
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)
and here admin django.contrib import admin
# register models here. .models import choice, question, quiz class choiceinline(admin.tabularinline): model = choice = 3 class questioninline(admin.tabularinline): model = question = 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)
django doesn't support nested inlines without building functionality or using admin nested inline app not date.
a common pattern link directly child object new modeladmin
view.
create readonly field renders link.
class questioninline(admin.tabularinline): model = question = 3 readonly_fields = ('link', ) def link(self, obj): return '<a href="%s" target="_blank">edit</a>' % reverse('admin:appname_modelname_change', args=[obj.id]) link.allow_tags = true
Comments
Post a Comment