Smart Map in Python Tutorials - Registering our model in the admin.py file

EBISYS Tech
1

Welcome to the Smart Map In Python Tutorial Series. In the previous tutorial we added our GIS model in our models.py file.

In this video we will be registering our gis model in our admin.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 - Registering our model in the admin.py file (Video)

Bubble plot on open street map

Step 1: Open the Atom IDE from the start menu


Step 2: Open the terminal in the right pane


Step 3: Activate your virtual environment



Step 4: Make database migrations

Comment: In the previous tutorial we created our GIS model, and to save our changes made to the database we need to run our database migrations.

$ python manage.py makemigrations
$ python manage.py migrate

Step 5: Download the data file from github






Step 6: Install pandas and xlrd


$ pip install pandas
$ pip install xlrd

Step 7: Add leaflet to the list of installed apps in the settings.py file


Step 8: Add code to the admin.py file



from django.contrib import admin

from django.contrib.gis.geos import Point
from datetime import datetime
from leaflet.admin import LeafletGeoAdmin
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

from waterwatchapp.models import WaterConsumption

# Register your models here.

class WaterConsumptionAdmin(LeafletGeoAdmin):
    pass

admin.site.register(WaterConsumption, WaterConsumptionAdmin)

df_excelReader = pd.read_excel('/home/edwin/Documents/data/waterwatch_clean2.xlsx', sheetname='Sheet1')

for index, row in df_excelReader.iterrows():
    Id = index
    Suburb = row['Suburb']
    NoOfSingleResProp = row['Number of single-residential properties_number']
    AvgMonthlyKL = row['Oct 2017\nkl/month']
    AvgMonthlyKLPredicted = 0
    PredictionAccuracy = 0
    Month = row['Month']
    Year = row['Year']
    DateTime = datetime.now()
    Longitude = row['Longitude']
    Latitude = row['Latitude']

    WaterConsumption(Id=Id, Suburb=Suburb, NoOfSingleResProp=NoOfSingleResProp,
                 AvgMonthlyKL=AvgMonthlyKL, AvgMonthlyKLPredicted=AvgMonthlyKLPredicted,
                 PredictionAccuracy=PredictionAccuracy, Month=Month, Year=Year,
                 DateTime=DateTime, geom=Point(Longitude, Latitude)).save()


Comment:

Modify the pd.read_excel() function to point to the location of the data file you downloaded from gitbub.

Step 9: Make database migrations

$ python manage.py makemigrations
$ python manage.py migrate

Step 10: Start the python server


$ python manage.py runserver

Step 11: Open the django admin portal

localhost:8000/admin

Step 12: Verify that the data has been inserted into the WaterConsumption table



Step 13: Click on a suburb to verify that the data is correct



Step 14: Update the geom field by changing the GPS coordinates of the suburb, to verify that the database update operation works


Select a marker



Place the marker on the new position and click save




Verify that the record has been updated




That is about all we need to cover for registering our GIS model in the admin.py file. I hope you enjoyed it and it will add some value to the projects you are currently busy with.












Post a Comment

1Comments
Post a Comment