当我创建一个模型时生成模型(对于日历应用程序)
我已经设置了几个模型,您很快就会在我的代码中看到。这些模型是纪元、年、月、日和小时。
我希望,当我创建一个纪元时,它会创建一组年,这些年又创建了几个月,又创建了几天,又创建了几小时。我相信,如果有人能帮助我使用从时代到年份步骤的方法论,我可以弄清楚其中的大部分内容。
目标是让 CurrentTime 模型能够逐步执行所有其他模型(基本上就像时钟一样)。
如果您想知道,我将尝试以此为基础制作一个简单的网络游戏,这就是为什么这些值与常规日历有点不同!
这是我的 models.py 文件:
from django.db import models
# Create your models here.
DAY_LENGTH = 24 #1 day is 24 hours
MONTH_LENGTH = 24 #1 month is 24 days
MONTH_CHOICES = (
(1, 'January'),
(2, 'Febuary'),
(3, 'March'),
(4, 'April'),
(5, 'May'),
(6, 'June'),
(7, 'July'),
(8, 'August'),
(9, 'September'),
(10, 'October'),
(11, 'November'),
(12, 'December'),
(13, 'Extravember'),
(14, 'Othertober'),
)
DAY_CHOICES = (
(1, 'Monday'),
(2, 'Tuesday'),
(3, 'Wednesday'),
(4, 'Thursday'),
(5, 'Friday'),
(6, 'Saturday'),
)
YEAR_LENGTH = MONTH_CHOICES.length #1 year contains
ERA_LENGTH = 9999 #1 era is 9999 years #one of every month
NUMBER_OF_BLOCKS = 6 #Number of discreet actions programmable for a day
BLOCK_LENGTH = DAY_LENGTH / NUMBER_OF_BLOCKS
class Era(models.Model):
#Era number
value = models.AutoField()
#Name of the Era
name = models.CharField(max_length=50)
def __unicode__(self):
return "Era of " + self.name
class Year(models.Model):
#Year number
value = models.PositiveSmallIntegerField(max_value=9999)
#Era the year belongs to
era = models.ForeignKey('Era')
length = YEAR_LENGTH
def __unicode__(self):
return "Year " + self.value + self.era
class Month(models.Model):
#Should return name of month
value = models.PositiveSmallIntegerField(
choices=MONTH_CHOICES)
#Year that the month fits into
year = models.ForeignKey(Year)
def __unicode__(self):
return self.value " of " + self.year
class Day(models.Model):
#Should give the name of the day
name = models.PositiveSmallIntegerField(
choices = DAY_CHOICES)
#Month that the day belongs to
month = models.ForeignKey('Month')
#Day number, dependant on month length
value = models.PositiveSmallIntegerField(
max_value = month.length)
def __unicode__(self):
return self.name + ", day " + self.value + " of " + self.month
class Hour(models.Model):
value = models.PositiveSmallIntegerField(
max_value = DAY_LENGTH)
day = models.ForeignKey('Day')
def __unicode__(self):
return self.value + ":00, " + self.day
class CurrentTime(models.Model):
hour = ForeignKey('Hour')
day = ForeignKey('Day')
month = ForeignKey('Month')
year = ForeignKey('Year')
era = ForeignKey('Era')
def __unicode__(self): #!!will only work for current config!
return self.hour
我确信它相当混乱......但无论如何,请帮我弄清楚我的代码!以及您想给我的任何其他帮助,我也很感激!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将数据结构存储在 6 个表中是多余的。您不会将字符串“abc”存储在三个不同的表中。所以我认为你的设计中有一些地方是错误的。
相反,请尝试尽可能简单地写出您的问题,并将其与您的解决方案一起发布到 SO。
您的实现有一定的替代方案,
导入此
如果您想 - 反对任何建议;-) - 使用上述模型,覆盖
save
模型的方法。Storing a datastructure in 6 tables is overkill. You would not store a string "abc" in three different tables. So something in your design is, in my opinion, wrong.
Try instead to write your problem as simple as possible and post it to along with your solution to it here at SO.
There are sure alternatives to your implementation and
import this
If you want to - against any advice ;-) - use your above stated Models, override the
save
method of your models.