Sahand
Sahand

Reputation: 8360

How to prevent abstract model class from being included in migrations

I've made two very simple models with the help of an abstract model:

class Activity(models.Model):

    name = models.CharField(max_length = 100)

    def __str__(self):
        return self.name
    __repr__ = __str__

class CardioActivity(Activity):
    pass

class LiftActivity(Activity):
    pass

The Activity model is just an abstract model that is not intended to be used for anything, only to save me writing the same stuff twice. But when I makemigrations, it creates a database table for it:

(workout) Sahands-MBP:workout sahandzarrinkoub$ python manage.py makemigrations
Migrations for 'workoutcal':
  workoutcal/migrations/0002_activity_cardioactivity_liftactivity_workout.py
    - Create model Activity ### HERE
    - Create model Workout
    - Create model CardioActivity
    - Create model LiftActivity

It seems suboptimal to create a table that is never going to be used by me. Is there a standard way of preventing this from happening?

Upvotes: 0

Views: 25

Answers (1)

dirkgroten
dirkgroten

Reputation: 20692

That's because you didn't declare it to be abstract:

class Activity(models.Model):
    name = ...

    class Meta:
         abstract = True

Upvotes: 2

Related Questions