-
Notifications
You must be signed in to change notification settings - Fork 0
Add new resource types or other objects that persist in the Hydroshare database
This document is part of the Hydroshare Developers' Guide.
hs_app/models.py
hs_app/admin.py
hs_app/templates/pages/myresourcetype.html
A number of Hydroshare extensions define new Resource types. Resource types define persistent objects in the database schema that are considered first-level objects in Hydroshare. Defining a new resource type involves a little code and a couple of maintenance steps.
-
First we will edit
models.py
. This will create the datatype to the database.from django.contrib.gis.db import models from mezzanine.pages.models import Page from hs_core.models import HydroshareResourceMixin
class MyResourceType(Page, HydroshareResourceMixin): pass
-
The next thing is to register this datatype with the Django admin interface. Edit
admin.py
:from mezzanine.pages.admin import PageAdmin from django.contrib import admin from .models import MyResourceType
admin.register(MyResourceType, PageAdmin)
Now that we have a new resource type, we need to create the tables in the database.
-
First, change directories to the main
hydroshare/
directory. You will then type the following, wherehs.my_hydroshare_app
is the name of your Hydroshare extension. This will create a new directory,hs/my_hydroshare_app/migrations
and a single file, which will update the database.$ python manage.py schemamigration --init hs.my_hydroshare_app
-
To update the database:
$ python manage.py migrate hs.my_hydroshare_app
-
Anytime you add fields to your resource or change a field's datatype or default value, it is a good idea to run the following code to catch any new changes. Django's migration mechanism also catches common "errors" in database changes, such as forgetting to allow NULL values in nullable fields or providing sensible defaults.
$ python manage.py schemamigration --auto hs.my_hydroshare_app $ python manage.py migrate hs.my_hydroshare_app
TODO Explain what is in the HydroshareResourceMixin.
TODO Explain what's in a Page.
TODO Explain Mezzanine's neat feature about finding pages