具有自我实例的Django模型方法 [英] Django model method with self instance

查看:71
本文介绍了具有自我实例的Django模型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些功能逻辑移入模型的方法中(而不是在视图中),我认为它属于该模型:

I am trying to move some functional logic into a model's method (instead of in the views), where I think it belongs:

class Spans(models.Model):
    snow = models.IntegerField()
    wind = models.IntegerField()
    exposure = models.CharField(max_length=1)
    module_length = models.IntegerField()
    max_roof_angle = models.IntegerField()
    building_height = models.IntegerField()
    importance_factor = models.IntegerField()
    seismic = models.DecimalField(max_digits=4, decimal_places=2)
    zone = models.IntegerField()
    profile = models.CharField(max_length=55)
    tilt_angle = models.IntegerField()
    span = models.DecimalField(max_digits=18, decimal_places=15)

    def get_spans(self, snow_load, wind_speed, module_length):
        """
        Function to get spans.
        """
        spans = self.objects.filter(
            snow=snow_load,
            wind=wind_speed,
            exposure='B',
            module_length__gte=module_length,
            max_roof_angle=45,
            building_height=30,
            importance_factor=1,
            seismic=1.2,
            zone=1,
            profile='worst'
        ).order_by('snow', 'module_length', 'span')
        return spans

但是,我不太确定如何称呼它。在外壳程序中,我尝试过:

However, I am a little unsure how to call it. In the shell I have tried:

>>> possible_spans = Spans.get_spans(0, 85, 54)

>>> possible_spans = Spans.get_spans(Spans, 0, 85, 54)

但出现错误:

TypeError: unbound method get_spans() must be called with Spans instance as first argument (got int instance instead) 

TypeError: unbound method get_spans() must be called with Spans instance as first argument (got ModelBase instance instead)  

我知道我在这里缺少python逻辑中的一些基本知识,但是我不确定它是什么。

I know I am missing something fundamental in python logic here, but I'm not sure what it is.

任何帮助将不胜感激。

推荐答案

方法必须与实例一起调用,因为方法调用使其成为默认的self arg。例如,

Method needs to be called either with the instance, as the method call makes it as the default self arg. For example,

instance = Spans.objects.get(id=1)
instance.get_spans(0, 85, 54)

或者如果您想使用类本身进行调用。您必须手动传递实例

Or if you can want to call using class itself. You would have to pass the instance manually

instance = Spans.objects.get(id=1)
Spans.get_spans(instance, 0, 85, 54)

更新:
您在寻找什么可以使用静态方法来实现

Update: What you are looking for could be achieved using a static method

@staticmethod
def get_spans(snow_load, wind_speed, module_length):
        """
        Function to get spans.
        """
        spans = Spans.objects.filter(
            snow=snow_load,
            wind=wind_speed,
            exposure='B',
            module_length__gte=module_length,
            max_roof_angle=45,
            building_height=30,
            importance_factor=1,
            seismic=1.2,
            zone=1,
            profile='worst'
        ).order_by('snow', 'module_length', 'span')
        return spans

现在您可以直接这样做,

Now you could directly do,

Spans.get_spans(0, 85, 54)

这篇关于具有自我实例的Django模型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆