Smart Map in Python Tutorials - Updating our Django models.py file

EBISYS Tech
1

Welcome to the Smart Map In Python Tutorial Series. In the previous tutorial we added our spatial database to our Django back-end. We first installed two external Django libraries called django-leaflet and geojson. Then we included our app in the list of installed apps. We then updated our default database settings and changed our database engine to postgis. Lastly we created a Django superuser account and we verified that the superuser account existed by opening the Django admin console.

In this tutorial we will be adding our GIS model, by updating our Django models.py file.

If you want to check out the video, click on the link below. Remember to Like and Subscribe and hit the Notification button to make sure you're aware of the latest video we will publish.

Smart Map in Python Tutorials 2018 - Updating our Django models.py file (Video)

Bubble plot on open street map

Step 1: Open the Atom IDE from the start menu



Step 2: Open the models.py file


Step 3: Import a gis model

Remove the first import statement. Because the first import statement imports a default Django model. We will be creating a model for a spatial database, so we need to import a GIS model instead. 

Add the below code:

$ from django.contrib.gis.db import models


Step 4: Create a model class

The class represents the schema of the table. The table name in the database will be the same name as the class name by default.

Add the below code:

class WaterConsumption(models.Model):
  Id = models.IntegerField(primary_key=True)
  Suburb = models.CharField(max_length=100) 
  NoOfSingleResProp = models.IntegerField()
  AvgMonthlyKL = models.IntegerField()
  AvgMonthlyKLPredicted = models.IntegerField()
  PredictionAccuracy = models.IntegerField()
  Month = models.CharField(max_length=50)
  Year = models.IntegerField()
  DateTime = models.DateTimeField()
  geom = models.PointField()

  def __str__(self):
        return self.Suburb

  class Meta:
        verbose_name_plural = 'WaterConsumption'


Comments: 
  • We will be adding a primary key to the Id field.
  • All our string values will be represented using the CharField data type a length can also be set per CharField.
  • The Month field will store the actual name of the month as our average consumption values will be grouped by month.
  • The DateTime field will store the capture date, every row off data will have a unique capture date.
  • The geom field will store the geolocation of every suburb, which is represented as gps coordinates in latitude and longitude.
  • The alias of the Consumption table will be Suburb.
  • And the class metadata just displays the actual name of the class.



That is about all we need to cover for updating our Django models.py file. I hope you enjoyed it and it will add some value to the projects you are currently busy with.

Stay tuned for the next tutorial in the Smart Map in Python Tutorial Series where we will be updating our Django admin.py file.





















Post a Comment

1Comments
  1. Thanks, I'm glad you like it. I will be posting allot more soon. I just have been busy for the last few months.

    ReplyDelete
Post a Comment