LoginSignup
1
0

More than 3 years have passed since last update.

Django で models.Modelにクラスメソッドを追加する

Posted at

はじめに

Modelを継承してクラスメソッドを追加したかったのですが、手こずったので備忘録。

やりたかったこと

  • 各modelに、条件に合致するオブジェクトが存在するか確認するis_existsメソッドを追加

前提

  • Django == 3.1.3

How to

Pythonでクラスメソッドを追加する方法は複数あります
TestClass に test_method を追加したい場合、

パターン1

def test_method(cls):
   ...

TestClass.test_method = classmethod(test_method)

TestClass.test_method()

パターン2

class MyTestClass(TestClass):
    @classmethod
    def test_method(cls):
        ...

MyTestClass.test_method()

今回は、パターン2のクラスを継承して追加する方法で行います。

コード

models.py
from django.db import models
class MyModel(models.Model):
    @classmethod
    def is_exists(cls, **kwargs):
        try:
            obj = cls.objects.get(**kwargs)
        except cls.DoesNotExist:
            return None
        else:
            return obj
    class Meta:
        abstract = True

class Hogehoge(MyModel):
    name = models.CharFields(max_length=100)
    ...

注意すべき点は Metaクラスのabstractです。
models.pyに記述した場合、このクラスそのものがModelだと認識されてしまうため、エラーが出ます。(The field ** clashes with the field ** from model **.)
そのため、Djangoに抽象モデルだと認識してもらう必要があり、それがabstractです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0