Skip to content

Add new resource types or other objects that persist in the Hydroshare database

Stephanie Mills edited this page Jan 15, 2014 · 6 revisions

This document is part of the Hydroshare Developers' Guide.

You will need to edit:

  • hs_app/models.py
  • hs_app/admin.py
  • hs_app/templates/pages/myresourcetype.html

For more information:

Defining a new resource type

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.

  1. 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

  2. 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)

Creating tables in the database

Now that we have a new resource type, we need to create the tables in the database.

  1. First, change directories to the main hydroshare/ directory. You will then type the following, where hs.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

  2. To update the database:

    $ python manage.py migrate hs.my_hydroshare_app

  3. 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