简体   繁体   English

在Django中创建相同的模型

[英]Create identical model in django

I am using django 1.10 with mysql. 我正在将django 1.10与mysql一起使用。 I am willing to have two tables in my db with the same fields. 我愿意在数据库中具有相同字段的两个表。

class Ticket(models.Model):
    listing = models.ForeignKey(Listing)
    ticketId = models.CharField(max_length=32)
    dateOfPosting = models.DateTimeField()
    seatNumber = models.PositiveIntegerField(null=True, blank=True)

class SoldTicket(models.Model):
    ### same fields here

What is the best way to do it? 最好的方法是什么?

Having two identical tables in your database suggests that you don't need them, a boolean field or some foreign key would most likely do the job. 数据库中有两个相同的表表明您不需要它们,布尔字段或某些外键很可能会完成此工作。

Hovewer, if you really want to have two identical models, you should look at abstract models . Hovewer,如果您真的想拥有两个相同的模型,则应查看抽象模型

class AbstractBase(models.Model):
    listing = models.ForeignKey(Listing)
    ticketId = models.CharField(max_length=32)
    ...

    class Meta:
        abstract = True

class Model1(AbstractBase):
    pass

class Model1(AbstractBase):
    pass

That way Model1 and Model2 will have same fields. 这样,Model1和Model2将具有相同的字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM