如何在Django中集成模型函数

时间:2015-10-29 02:20:46

标签: python django

我有一个名为UNIT的模型。我想创建一个函数,根据条件从模型中获取某些东西。

class Unit (models.Model):
    HType = (
        ('HQ', 'IFT Headquarters'),
        ('JTF', 'Joint Task Force'),
        ('GEO', 'Geographic Theater'),
        ('SHIP', 'Federation Vessel Chapter')
    )
    Unit_name = models.CharField(max_length=30)
    Hull_number = models.CharField(max_length=30)
    Hull_type = models.CharField(max_length=4, choices=HType)
    Parent_unit = models.ForeignKey("self", blank=True, null=True)
    Class = models.ForeignKey(Class)
    email = models.CharField(max_length=50, default='')
    Member = models.ManyToManyField(Member, through='MemberUnit')
    Address = models.CharField(max_length=500, default='')
    City = models.CharField(max_length=50, default='')
    State = models.CharField(max_length=2, default='')
    Zip = models.IntegerField()

    def getCO(self):
        from ifthq import models
        Posn = models.Position.objects.get(Position_name="Commanding Officer")
        CPosn = models.ClassPosition.objects.get(unit_class=self.Class, unit_position=Posn)
        MbrU = models.MemberUnit.objects.get(Unit=self, Position=CPosn)
        MbrR = models.MemberRank.objects.filter(Member=MbrU.Member).latest("Date_Effective")
        CO = MbrR.Rank.Short_name + ' ' + MbrU.Member.first_name + ' ' + MbrU.Member.last_name
        return CO

    def getstaff(self, position):
        from ifthq import models
        Posn = models.Position.objects.get(Position_name=position)
        CPosn = models.ClassPosition.objects.get(unit_class=self.Class, unit_position=Posn)
        MbrU = models.MemberUnit.objects.get(Unit=self, Position=CPosn)
        MbrR = models.MemberRank.objects.filter(Member=MbrU.Member).latest("Date_Effective")
        XO = MbrR.Rank.Short_name + ' ' + MbrU.Member.first_name + ' ' + MbrU.Member.last_name
        return XO

    def __str__(self):
        return self.Unit_name

Unit.getCO工作,Unit.getstaff()没有。

以下是观点:

@login_required
def unit_view(request, unit):
    try:
        unit_id = models.Unit.objects.get(Q(Hull_number=unit) | Q(Unit_name=unit))
    except unit_id.DoesNotExist:
        raise Http404("Poll does not exist")
    if unit_id.Hull_type == "GEO":
        ship_count = models.Unit.objects.filter(Parent_unit=unit_id).filter(Hull_type="SHIP").count()
        tf_count = models.Unit.objects.filter(Parent_unit=unit_id).filter(Hull_type="JTF").count()
        fenlisted = models.Subscription.objects.rankset("E").active().filter(Member__in=models.MemberUnit.objects.filter(Unit__Parent_unit=unit_id).values("Member")).count()
        fofficer = models.Subscription.objects.rankset("O").active().filter(Member__in=models.MemberUnit.objects.filter(Unit__Parent_unit=unit_id).values("Member")).count()
        fcivilian = models.Subscription.objects.rankset("C").active().filter(Member__in=models.MemberUnit.objects.filter(Unit__Parent_unit=unit_id).values("Member")).count()
    else:
        ship_count = 0
        tf_count = 0
        fenlisted = 0
        fofficer = 0
        fcivilian = 0
    enlisted = models.Subscription.objects.rankset("E").active().filter(Member__in=models.MemberUnit.objects.unitcrew(unit_id).values("Member")).count()
    officer = models.Subscription.objects.rankset("O").active().filter(Member__in=models.MemberUnit.objects.unitcrew(unit_id).values("Member")).count()
    civilian = models.Subscription.objects.rankset("C").active().filter(Member__in=models.MemberUnit.objects.unitcrew(unit_id).values("Member")).count()
    context = {'fcivilian': fcivilian, 'fofficer': fofficer,
               'fenlisted': fenlisted, 'fleet': ship_count,
               'jtf': tf_count, 'enlisted': enlisted,
               'officer': officer, 'civilian': civilian,
               'Unit': unit_id
               }
    return render(request, 'unit.html', context)

在模板中,我正在使用variable.getCO和`variable.getstaff(“Executive Officer”),我可能会在for循环中返回1行或更多行。

getCO工作,getstaff(“执行官”)没有。我如何让getstaff工作?

感谢。

1 个答案:

答案 0 :(得分:1)

您正在使用模板中的参数调用模型方法,这样就不可能了。

您应该按如下方式定义custom template filter并使用:

@register.filter
def getstaff(obj, position):
   # your code remains here
   # instead of self use obj and at the end return value

现在在模板上使用它:

{{ variable|getstaff:"Executive Officer" }}