简体   繁体   English

如何使用模型的功能?

[英]How can I use the Model's function?

I have a TypeModel model, and in it there is a : 我有一个TypeModel模型,其中有一个:

class TypeModel(models.Model):
    name = models.CharField(max_length=22)
    type = models.CharField(max_length=12)
    ch = models.CharField(max_length=44, null=True)

    def print(self, name, type):
        t = TypeModel.objects.create(name=name, type=type)
        print('success - ' + t.name)

I want to invoke the print method like this: 我想这样调用print方法:

class TypeModelCreateAPIView(APIView):
    permission_classes = [AllowAny]
    def post(self, request):

        TypeModel.print() # Can I invoke like this
        return Response(status=HTTP_200_OK, data='')

Whether I can invoke the function of model like this? 我是否可以像这样调用模型的功能? if not, how to realize the Class method of model? 如果不是,如何实现模型的Class方法?

You can use staticmethod: 您可以使用staticmethod:

@staticmethod
def print(name, type):
    t = TypeModel.objects.create(name=name, type=type)
    print('success - ' + t.name)

Yes, this can be done with the staticmethod annotation 是的,这可以通过staticmethod注释完成

class TypeModel(models.Model):
    name = models.CharField(max_length=22)
    type = models.CharField(max_length=12)
    ch = models.CharField(max_length=44, null=True)

    @staticmethod
    def print(name, type):
        t = TypeModel.objects.create(name=name, type=type)
        print('success - ' + t.name)    

Please note, that a static method does not have the implicit self as first argument. 请注意,静态方法没有隐式self作为第一个参数。

Currently print is defined as an instance method. 当前print被定义为实例方法。 You would have to create an instance of TypeModel to call it. 您必须创建TypeModel实例才能调用它。 Like: 喜欢:

model = TypeModel()
model.print()

Or you could use the @staticmethod decorator: 或者您可以使用@staticmethod装饰器:

@staticmethod
def print(name, type):
    t = TypeModel.objects.create(name=name, type=type)
    print('success - ' + t.name)  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 PySpark 的 Window function 到 model 指数衰减? - How can I use PySpark's Window function to model exponential decay? 如何在 Keras model 中使用加权 MSE 作为损失 function? - How can I use a weighted MSE as loss function in a Keras model? 如何在PTVS中使用IPython的搜索历史记录功能? - How can I use IPython's search history function in PTVS? 如何在 django 的 Replace function 中使用正则表达式 - How can I use regex in django's Replace function 如何使用预训练的 keras 模型作为模型添加函数的参数? - How to use a pretrained keras model to be a parameter to a Model's add function? 如何将表中的所有对象用作标签 - 在Django的Admin中输入模型? - How can I use all objects in a table as a label - input in a model in Django's Admin? 如何在子类化 django 的 model FileField 时使用关键字 arguments - How can I use keyword arguments when subclassing django's model FileField 在Python中,如何在第二个函数中使用函数的return语句而不使用全局变量? - In Python, how can I use a function's return statement within a second function without using global variables? 我可以使用机器学习模型作为优化问题的目标函数吗? - Can I use a machine learning model as the objective function in an optimization problem? 如何在同一模型中使用两个不同的模型序列化器? - How Can I Use Two Different Model Serializers With the Same Model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM