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

发布于 2024-11-11 04:08:46 字数 2941 浏览 0 评论 0 原文

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

我希望,当我创建一个纪元时,它会创建一组年,这些年又创建了几个月,又创建了几天,又创建了几小时。我相信,如果有人能帮助我使用从时代到年份步骤的方法论,我可以弄清楚其中的大部分内容。

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

我确信它相当混乱......但无论如何,请帮我弄清楚我的代码!以及您想给我的任何其他帮助,我也很感激!

I've set up several models, as you'll see shortly in my code. These models are Era, Year, Month, Day, and Hour.

I'd like for, when I create an Era, it creates a set of Years, which in turn create Months, which in turn create Days, which create Hours. I'm sure I can figure out most of this if someone could just help me with the methodology with say the Era to Year step.

The goal is then to have the CurrentTime model able to step through all the other models (like a clock basically).

In case you're wondering, I'm going to try and make a simple web based game off this, that's why the values are a bit different from a regular calendar!

So here's my models.py file:

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

I'm sure it's pretty messy... But in any case, please help me figure out my code! And any other help you'd like to give me, I appreciate too!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-11-18 04:08:46

将数据结构存储在 6 个表中是多余的。您不会将字符串“abc”存储在三个不同的表中。所以我认为你的设计中有一些地方是错误的。

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

您的实现有一定的替代方案,导入此

>>> 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 模型的方法。

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

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

If you want to - against any advice ;-) - use your above stated Models, override the save method of your models.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文