I have dfs with the following column headers:
Year JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC WIN SPR SUM AUT ANN ATTRIBUTE LOCATION
Each column contains random floating point value, except for ATTRIBUTE, which contains a value from this list:
[Max_temp, Min_temp, Mean_temp, Sunshine, Rainfall]
and LOCATION, which contains a value from this list:
[UK, England, Scotland, Wales]
I need to put it into the sqlite3 database that comes with django using the following models:
from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
class Location(models.Model):
LOCATIONS = (
('EN', 'England'),
('SC', 'Scotland'),
('WA', 'Wales'),
('UK', 'United Kingdom'),
)
location = models.CharField(max_length=2, choices=LOCATIONS)
class Meta:
verbose_name_plural = "Location"
def __str__(self):
return self.location
class Max_temp(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
year = models.IntegerField(
default=0,
validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
MONTH_OR_SEASON = (
("JAN", "January"),
("FEB", "February"),
("MAR", "March"),
("APR", "April"),
("MAY", "May"),
("JUN", "June"),
("JUL", "July"),
("AUG", "August"),
("SEP", "September"),
("OCT", "October"),
("NOV", "November"),
("DEC", "December"),
("WIN", "Winter"),
("SPR", "Spring"),
("SUM", "Summer"),
("AUT", "Autumn"),
("ANN", "Annual"),
)
month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)
class Meta:
verbose_name_plural = "Maximum Temperature"
def __str__(self):
return self.year
class Min_temp(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
year = models.IntegerField(
default=0,
validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
MONTH_OR_SEASON = (
("JAN", "January"),
("FEB", "February"),
("MAR", "March"),
("APR", "April"),
("MAY", "May"),
("JUN", "June"),
("JUL", "July"),
("AUG", "August"),
("SEP", "September"),
("OCT", "October"),
("NOV", "November"),
("DEC", "December"),
("WIN", "Winter"),
("SPR", "Spring"),
("SUM", "Summer"),
("AUT", "Autumn"),
("ANN", "Annual"),
)
month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)
class Meta:
verbose_name_plural = "Minimum Temperature"
def __str__(self):
return self.year
class Mean_temp(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
year = models.IntegerField(
default=0,
validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
MONTH_OR_SEASON = (
("JAN", "January"),
("FEB", "February"),
("MAR", "March"),
("APR", "April"),
("MAY", "May"),
("JUN", "June"),
("JUL", "July"),
("AUG", "August"),
("SEP", "September"),
("OCT", "October"),
("NOV", "November"),
("DEC", "December"),
("WIN", "Winter"),
("SPR", "Spring"),
("SUM", "Summer"),
("AUT", "Autumn"),
("ANN", "Annual"),
)
month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)
class Meta:
verbose_name_plural = "Mean Temperature"
def __str__(self):
return self.year
class Sunshine(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
year = models.IntegerField(
default=0,
validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
MONTH_OR_SEASON = (
("JAN", "January"),
("FEB", "February"),
("MAR", "March"),
("APR", "April"),
("MAY", "May"),
("JUN", "June"),
("JUL", "July"),
("AUG", "August"),
("SEP", "September"),
("OCT", "October"),
("NOV", "November"),
("DEC", "December"),
("WIN", "Winter"),
("SPR", "Spring"),
("SUM", "Summer"),
("AUT", "Autumn"),
("ANN", "Annual"),
)
month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)
class Meta:
verbose_name_plural = "Sunshine"
def __str__(self):
return self.year
class Rainfall(models.Model):
location = models.ForeignKey(Location, on_delete=models.CASCADE)
year = models.IntegerField(
default=0,
validators=[MaxValueValidator(9999), MinValueValidator(0)]
)
MONTH_OR_SEASON = (
("JAN", "January"),
("FEB", "February"),
("MAR", "March"),
("APR", "April"),
("MAY", "May"),
("JUN", "June"),
("JUL", "July"),
("AUG", "August"),
("SEP", "September"),
("OCT", "October"),
("NOV", "November"),
("DEC", "December"),
("WIN", "Winter"),
("SPR", "Spring"),
("SUM", "Summer"),
("AUT", "Autumn"),
("ANN", "Annual"),
)
month_or_season = models.CharField(max_length=3, choices=MONTH_OR_SEASON)
class Meta:
verbose_name_plural = "Rainfall"
def __str__(self):
return self.year
I'm a bit stumped on how to do this - I'm not sure if my model structure is right? And is there a way to structure the models so I don't have to repeat code? I need a database structured like so:
Location - Max_temp
- Min_temp
- Mean_temp
- Sunshine
- Rainfall
So each location has a Max_temp table, a Min_temp table etc...
The main thing tho, is how do I tell Django to put the pandas df into the database and where in the Django project do I put that code?
Any help appreciated!