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

我有一个带有一些训练模型的卡模型,每个训练有完全相同的数据:练习名称,重复..等等训练是7,一周中的每一天。 所以我用完全相同的数据编写了7个模型,这样在管理模型中我有7次训练,每次我都可以添加“+”多少次练习(使用内联)

有没有办法只编写一次锻炼模型,然后有可能在管理界面中多次添加锻炼模型? (每次添加锻炼时,我希望能够在相同的管理视图中添加锻炼名称,重复次数等)

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) 

感谢代码,现在它更清楚。 在我看来,所有的数据库结构(通过django ORM模型创建的)都是不正确的,基于DRY和构建数据库标准。

所以我会告诉你这个数据库应该如何找我。 只有一件事,你应该使用__unicode__方法而不是__str__ (链接)。

  • class Card - 没关系
  • class Exercise - 它仅提供名称字段。 为什么? 不知道,但我建议改变这个类的名字为ExerciseType ,我会告诉你为什么后面:)
  • Series, Repetitions, Executions, Rest - 与练习模型类似,每个模型只提供一个字段,每个字段的值不是很独特。
  • Exercise1-5 - 模型的名称应该是Exercise ,并且具有名为exercise_type或type的额外字段。
  • 往下看:

    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 ....
    

    所以,结果,我们有3个模型(表),而不是11,更简单的SQL查询(没有6 SQL连接。功能是一样的(我希望:))。如果你有任何问题,只要问我,我会尝试帮助。

    希望这可以帮助。

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

    上一篇: django: more models with the same fields and admin interface

    下一篇: Django admin: ordering inline based on FK'd property