重构我的django模型是否安全?

时间:2021-09-13 20:57:12

My model is similar to this. Is this ok or should I make the common base class abstract? What are the differcenes between this or makeing it abstract and not having an extra table? It seems odd that there is only one primary key now that I have factored stuff out.

我的模型与此类似。这样可以,还是应该将公共基类抽象化?这之间有什么不同之处或者是抽象而没有额外的表格?现在我只考虑了一个主键,这似乎很奇怪。

class Input(models.Model):
        details = models.CharField(max_length=1000)
        user = models.ForeignKey(User)
        pub_date = models.DateTimeField('date published')
        rating = models.IntegerField()

        def __unicode__(self):
            return self.details

    class Case(Input):
        title  = models.CharField(max_length=200)
        views = models.IntegerField()

    class Argument(Input):
        case = models.ForeignKey(Case)
        side = models.BooleanField()

is this ok to factor stuff out intpu Input? I noticed Cases and Arguments share a primary Key.

这可以将intpu输入?我注意到Case和Arguments共享一个主键。

like this:

    CREATE TABLE "cases_input" (
        "id" integer NOT NULL PRIMARY KEY,
        "details" varchar(1000) NOT NULL,
        "user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
        "pub_date" datetime NOT NULL,
        "rating" integer NOT NULL
    )
    ;
    CREATE TABLE "cases_case" (
        "input_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "cases_input" ("id"),
        "title" varchar(200) NOT NULL,
        "views" integer NOT NULL
    )
    ;
    CREATE TABLE "cases_argument" (
        "input_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "cases_input" ("id"),
        "case_id" integer NOT NULL REFERENCES "cases_case" ("input_ptr_id"),
        "side" bool NOT NULL
    )

1 个解决方案

#1


From: django web site

来自:django网站

Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.

当您想要将一些公共信息放入许多其他模型时,抽象基类非常有用。编写基类并在Meta类中放置abstract = True。然后,此模型将不用于创建任何数据库表。相反,当它用作其他模型的基类时,其字段将添加到子类的字段中。

#1


From: django web site

来自:django网站

Abstract base classes are useful when you want to put some common information into a number of other models. You write your base class and put abstract=True in the Meta class. This model will then not be used to create any database table. Instead, when it is used as a base class for other models, its fields will be added to those of the child class.

当您想要将一些公共信息放入许多其他模型时,抽象基类非常有用。编写基类并在Meta类中放置abstract = True。然后,此模型将不用于创建任何数据库表。相反,当它用作其他模型的基类时,其字段将添加到子类的字段中。