0

我有一个带有一些锻炼模型的卡片模型,每次锻炼都有完全相同的数据:锻炼名称、重复次数..等锻炼是 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) 
4

1 回答 1

2

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

因此,我将向您展示该数据库应该如何查找我。只有一件事,你应该使用__unicode__方法而不是__str__链接)。

  1. class Card- 没关系
  2. class Exercise- 它只提供名称字段。为什么 ?不知道,但是我建议把这个类的名字改成ExerciseType,我稍后会告诉你为什么:)
  3. Series, Repetitions, Executions, Rest- 类似于Exercise模型,每个模型只提供一个字段,每个字段的值不是很唯一。
  4. 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连接。功能是相同的(我希望 :))。如果您有任何问题,只要问我,我会尽力帮助。

希望这可以帮助。

于 2013-10-13T14:45:07.797 回答