django: more models with the same fields and admin interface

I have a card model with some workouts models, each workouts has exactly the same data: exercise name, repetitions..etc The workouts are 7, one for each day of the week. So I wrote 7 models with exactly the same data, in this way in the admin model I have 7 workouts and for each I can add "+" how many exercises I want (using inlines)

Is there a way to write the workout model only once and then have the possibility to add it many times in the admin interface? (each time I add a workout, I would like to be able to add exercise name, repetitions etc.. in the same admin view)

class Card(models.Model):

    number = models.IntegerField()
    name = models.CharField(max_length=50)
    surname = models.CharField(max_length=50)  
    trainer = models.CharField(max_length=50, blank=True, null=True)  

    #Card status
    creation = models.DateField(auto_now_add=True)
    expiration = models.DateField(blank=True, null=True)

    #Member status
    subscription = models.DateField(blank=True, null=True)

    def __str__(self):
        return u'%s %s' % (self.surname, self.name)

    class Meta:
        unique_together = (("number"),)



class Exercise(models.Model):

    name = models.CharField(max_length=50)

    def __str__(self):
        return u'%s' % (self.name)


class Series(models.Model):

    number = models.IntegerField()

    def __str__(self):
        return u'%s' % (self.number)

    class Meta:
        verbose_name = 'Series'
        verbose_name_plural = 'Series'


class Repetitions(models.Model):

    number = models.IntegerField()

    def __str__(self):
        return u'%s' % (self.number)

    class Meta:
        verbose_name = 'Repetition'
        verbose_name_plural = 'Repetitions'


class Executions(models.Model):

    mode = models.CharField(max_length=50)

    def __str__(self):
        return u'%s' % (self.mode)

    class Meta:
        verbose_name = 'Execution'
        verbose_name_plural = 'Executions'


class Rest(models.Model):

    time = models.IntegerField()

    def __str__(self):
        return u'%s' % (self.time)

    class Meta:
        verbose_name = 'Rest'
        verbose_name_plural = 'Rest'


class Exercise1(models.Model):

    card = models.ForeignKey(Card)
    exercise = models.ForeignKey(Exercise)
    series = models.ForeignKey(Series)
    repetitions = models.ForeignKey(Repetitions)
    executions = models.ForeignKey(Executions)
    rest = models.ForeignKey(Rest)

    class Meta:
        verbose_name = 'Exercise'
        verbose_name_plural = 'Workout 1'

    def __str__(self):
        return u'%s' % (self.exercise) 

class Exercise2(models.Model):

    card = models.ForeignKey(Card)
    exercise = models.ForeignKey(Exercise)
    series = models.ForeignKey(Series)
    repetitions = models.ForeignKey(Repetitions)
    executions = models.ForeignKey(Executions)
    rest = models.ForeignKey(Rest)

    class Meta:
        verbose_name = 'Exercise'
        verbose_name_plural = 'Workout 2'

    def __str__(self):
        return u'%s' % (self.exercise) 


class Exercise3(models.Model):

    card = models.ForeignKey(Card)
    exercise = models.ForeignKey(Exercise)
    series = models.ForeignKey(Series)
    repetitions = models.ForeignKey(Repetitions)
    executions = models.ForeignKey(Executions)
    rest = models.ForeignKey(Rest)

    class Meta:
        verbose_name = 'Exercise'
        verbose_name_plural = 'Workout 3'

    def __str__(self):
        return u'%s' % (self.exercise) 


class Exercise4(models.Model):

    card = models.ForeignKey(Card)
    exercise = models.ForeignKey(Exercise)
    series = models.ForeignKey(Series)
    repetitions = models.ForeignKey(Repetitions)
    executions = models.ForeignKey(Executions)
    rest = models.ForeignKey(Rest)

    class Meta:
        verbose_name = 'Exercise'
        verbose_name_plural = 'Workout 4'

    def __str__(self):
        return u'%s' % (self.exercise) 


class Exercise5(models.Model):

    card = models.ForeignKey(Card)
    exercise = models.ForeignKey(Exercise)
    series = models.ForeignKey(Series)
    repetitions = models.ForeignKey(Repetitions)
    executions = models.ForeignKey(Executions)
    rest = models.ForeignKey(Rest)

    class Meta:
        verbose_name = 'Exercise'
        verbose_name_plural = 'Workout 5'

    def __str__(self):
        return u'%s' % (self.exercise) 

thanks for code, now it's more clearly. In my opinion all your database structure ( created through django ORM models ) is incorrect, based on DRY and build database standards.

So I will show you how this database should looks for me. Only one more thing, you should using __unicode__ method instead of __str__ (link).

  • class Card - it's ok
  • class Exercise - it's provide only name field. why ? don't know, but I suggest to change the name of this class to ExerciseType , and I will tell you why later :)
  • Series, Repetitions, Executions, Rest - similar to Exercise model, provides only one field per model, and value of the each field is not very unique.
  • Exercise1-5 - name of the model should be Exercise , and has extra field named exercise_type, or type.
  • Look below:

    class Card(models.Model):
        number = models.IntegerField()
        name = models.CharField(max_length=50)
        surname = models.CharField(max_length=50)  
        trainer = models.CharField(max_length=50, blank=True, null=True)  
        #Card status
        creation = models.DateField(auto_now_add=True)
        expiration = models.DateField(blank=True, null=True)
        #Member status
        subscription = models.DateField(blank=True, null=True)
    
        def __unicode__(self):
            return u'%s %s' % (self.surname, self.name)
    
        class Meta:
            unique_together = (("number"),)
    
    class ExerciseType(models.Model):
    
        name = models.CharField(max_length=50)
    
        def __unicode__(self):
            return u'%s' % (self.name)
    
    class Exercise(models.Model):
        type = models.ForeignKey(ExerciseType)
        card = models.ForeignKey(Card)
        #instead of FK, better solutions is to use normal value-field 
        series = models.IntegerField()
        repetitions = models.IntegerField()
        executions = models.CharField()
        rest = models.IntegerField()
        #here comes methods like __unicode__ etc ....
    

    So, as a result, we have 3 models ( tables ) instead of 11, much simpler sql query ( without 6 SQL joins. Functionality is the same ( I hope :) ).If you have any question, just ask me I will try to help.

    hope this helps.

    链接地址: http://www.djcxy.com/p/65426.html

    上一篇: 如何为多种类型的问题创建django管理内联?

    下一篇: django:更多的模型具有相同的字段和管理界面