這是我的模型:Django和2個問題
from django.db import models
from depot.models import Subject, Student
# Create your models here.
class Credit(models.Model):
subject = models.ForeignKey(Subject)
number = models.IntegerField()
first_level_questions = models.IntegerField(default=8)
second_level_questions = models.IntegerField(default=8)
third_level_questions = models.IntegerField(default=4)
class Question(models.Model):
credit = models.ForeignKey(Credit)
question = models.CharField(max_length=200)
level_choice = (
(1, u'1'),
(2, u'2'),
(3, u'3'),
)
level = models.IntegerField(choices=level_choice)
a = models.CharField(max_length=200, blank=True)
b = models.CharField(max_length=200, blank=True)
c = models.CharField(max_length=200, blank=True)
d = models.CharField(max_length=200, blank=True)
right_answer_choices = (
(1, u'a'),
(2, u'b'),
(3, u'c'),
(4, u'd'),
)
right_answer = models.IntegerField(choices=right_answer_choices, blank=True)
class Answer(models.Model):
question = models.ForeignKey(Question)
student = models.ForeignKey(Student)
answer = models.CharField(max_length=400)
right = models.BooleanField(default=None)
def __unicode__(self):
return self.answer
class Mark(models.Model):
student = models.ForeignKey(Student)
credit = models.ForeignKey(Credit)
total = models.IntegerField()
這是我的admin.py:
from django.contrib import admin
from credits.models import Credit, Question
class QuestionInline(admin.StackedInline):
model = Question
extra = 0
class CreditAdmin(admin.ModelAdmin):
list_display = ['subject', 'number']
inlines = [QuestionInline,]
admin.site.register(Credit, CreditAdmin)
我解決我的第一個問題。謝謝。 我的第二個問題: 我想在我的標記模型中有一個方法。 你知道嗎:
def total_mark(self):
return Answer.objects.filter(student=self.student, right=True).count()
但是信用呢?我需要準確的學生和確切的學分。
對不起,我還沒有在你的第二個問題清晰。如果你有單獨的問題或更多的問題,你應該創建單獨的問題,而不是更容易找到。你也應該發佈你如何解決你的第一個問題,以防其他人遇到類似問題。 – aganders3 2011-12-21 18:10:05