guystart guystart - 1 month ago 14
MySQL Question

Create identical model in django

I am using django 1.10 with 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?

Answer

Having two identical tables in 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 moels.

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.