Generating models when I create one model (for a calendar app)
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!
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.
精彩评论