[英]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. 我有一个带有一些锻炼模型的卡片模型,每个锻炼都有完全相同的数据:锻炼名称,重复...等锻炼是7,每周一天锻炼。 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) 所以我用完全相同的数据编写了7个模型,这样在管理模型中我有7个锻炼,每个我可以添加“+”我想要多少个练习(使用内联)
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. 在我看来,基于DRY和构建数据库标准,所有数据库结构(通过django ORM模型创建)都是不正确的。
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 ). 只有一件事,你应该使用__unicode__
方法而不是__str__
( 链接 )。
class Card
- it's ok class Card
- 没关系 class Exercise
- it's provide only name field. class Exercise
- 它只提供名称字段。 why ? 为什么? don't know, but I suggest to change the name of this class to ExerciseType
, and I will tell you why later :) 不知道,但我建议将这个类的名称改为ExerciseType
,我会告诉你为什么以后:) Series, Repetitions, Executions, Rest
- similar to Exercise model, provides only one field per model, and value of the each field is not very unique. Series, Repetitions, Executions, Rest
- 类似于Exercise模型,每个模型只提供一个字段,每个字段的值不是很独特。 Exercise1-5
- name of the model should be Exercise
, and has extra field named exercise_type, or type. Exercise1-5
- 模型的名称应为Exercise
,并且具有名为exercise_type或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. 所以,结果,我们有3个模型(表)而不是11个,更简单的sql查询(没有6个SQL
连接。功能是相同的(我希望:))。如果你有任何问题,请问我会尝试帮助。
hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.