开发者

Django - Creating Models using a factory method

开发者 https://www.devze.com 2023-04-02 20:06 出处:网络
I need to create those three models: class Model_1(models.Model): def get_second_element(self): try: return Model_1.objects.all()[1]

I need to create those three models:

class Model_1(models.Model):
    def get_second_element(self):
        try:
            return Model_1.objects.all()[1]
        except:
            return None

class Model_2(models.Model):
     def 开发者_StackOverflowget_second_element(self):
        try:
            return Model_2.objects.all()[1]
        except:
            return None

class Model_3(models.Model):
     def get_second_element(self):
        try:
            return Model_3.objects.all()[1]
        except:
            return None

How do I write a factory method that gets the name of the model to be created, and creates a model class with that name, and with the function get_second_element that query the DB of this model?

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消