反向关系OnetoOne Django模板

时间:2016-05-10 23:43:41

标签: python django

我正试图在django模板中遍历OneTOne关系,但是结果不是预期的结果,即使关系确实存在但是我得到None因此我不知道为什么。< / p>

models.py

# Create your models here.

class Duration(models.Model):
    """
    """

    REG_DURATION_CHOICES = (
        ('y', 'YEAR'),
        ('1', 'FIRST SEMESTER'),
        ('2', 'SECOND SEMESTER'),
        ('s', 'SHORT TERM'),
    )
    EXT_DURATION_CHOICES = (
        ('d', 'DIPLOMA'),
        ('p', 'PATHWAY'),
        ('g', 'POST-GRADUATE'),
        ('o', 'OTHER'),
    )
    DURATION_CHOICES = (
        ('w', 'WEEKS'),
    ) + REG_DURATION_CHOICES + EXT_DURATION_CHOICES

    program         = models.ForeignKey(Program, null = True)
    period          = models.CharField(max_length = 1, choices = DURATION_CHOICES, blank = True, null = True)
    academic_period = models.ForeignKey(AcademicPeriod, null = True)
    weeks           = models.IntegerField(null = True, blank = True, default = 0)


class Comission(models.Model):
    duration = models.ForeignKey(Duration, related_name="%(class)s")
    comission = MoneyField(
        max_digits=10, decimal_places=2,
        default_currency='USD', null=True, blank=True)
    note = models.TextField(null=True, blank=True)

    class Meta:
        abstract = True


class BoardingComission(Comission):
    returning_student_comission = MoneyField(
        max_digits=10, decimal_places=2,
        default_currency='USD', null=True, blank=True)

    def get_comission(self, registration):
        # registration = get_object_or_404(Registration, pk=registration_id)
        if registration.returning_student:
            comission = self.returning_student_comission
        else:
            comission = self.comission
        return comission

views.py

@login_required
def finances(request):

    durations = Duration.objects.select_related(
        'program','program__category').prefetch_related('boardingcomission').all()
    boarding_comissions = durations.boarding()


    request_context = {
        'boarding_comissions': boarding_comissions,
    }

    return render(request, 'finances/finances.html', request_context)

template.html

// THIS FAILS
{% for c in boarding_comissions %}
<a href="#" data-value="{{ c.boardingcomission }}"</a>
{% endfor %}



输出:

finances.BoardingComission.None

0 个答案:

没有答案