guystart
guystart

Reputation: 5

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?

Upvotes: 0

Views: 323

Answers (1)

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.

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.

Upvotes: 4

Related Questions