创建一个模型时生成模型(对于日历应用程序)

时间:2011-05-30 11:31:51

标签: django django-models model calendar

我已经设置了几个模型,您很快就会在我的代码中看到。这些模型是时代,年,月,日和小时。

我想,当我创造一个时代时,它会创建一组年份,而这些年份又创造了几个月,这反过来创造了几天,创造了几个小时。我敢肯定,如果有人可以帮我解决方法问题,那么我可以弄明白这一点,并说“时代到年”。

目标是让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

我确定它非常混乱......但无论如何,请帮我弄清楚我的代码!还有你想给我的任何其他帮助,我也很感激!

1 个答案:

答案 0 :(得分:1)

将数据结构存储在6个表中是过度的。您不会在三个不同的表中存储字符串“abc”。因此,在我看来,您设计中的某些内容是错误的。

请尝试尽可能简单地编写您的问题,并将其与您的解决方案一起发布到此处。

您的实施有可靠的替代方案import this

>>> import this
...
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
...

如果你想 - 反对任何建议;-) - 使用你上面提到的模型,覆盖模型的 save方法。