django-edumetadata v0.1 documentation

Registering Models

Registering models in settings.py

It is nice to not have to modify the code of applications to add a relation to metadata. You can therefore do all the registering in settings.py. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
EDUMETADATA_SETTINGS = {
    'FK_REGISTRY': {
        'grade': {
            'simpletext.SimpleTextOne': (
                'primary_grade',
                {'name': 'secondary_grade', 
                'related_name': 'simpletext_secondary_grades'}
            )
        },
        'subject': {'simpletext.SimpleTextOne': 'subject'},
        'education_category': {'simpletext.SimpleTextOne': 'education_category'},
        'alternate_type': {'simpletext.SimpleTextOne': 'alternate_type'},
        'geologic_time': {'simpletext.SimpleTextOne': 'geologic_time'},
    },
    'M2M_REGISTRY': {
        'grade': {
            'simpletext.SimpleTextTwo': (
                'grades',
                {'name': 'secondary_grades', 
                'related_name': 'simpletexttwo_secondary_grades'}
            )
        },
        'subject': {'simpletext.SimpleTextTwo': 'subject'},
        'education_category': {'simpletext.SimpleTextTwo': 'education_categories'},
        'alternate_type': {'simpletext.SimpleTextTwo': 'alternate_types'},
        'geologic_time': {'simpletext.SimpleTextTwo': 'geologic_times'},
    }
}

The FK_REGISTRY and M2M_REGISTRY are dictionaries whose keys are any of the models in Django-Metadata. The values are a Relationship Definition.

You only need to specify the specific relationship you need. If aren’t using EducationalGrade, you don’t need to include a education_grade key.

Note

The default values for FK_REGISTRY and M2M_REGISTRY are really closer to:

EDUMETADATA_SETTINGS = {
    'FK_REGISTRY': {
        'grade': {},
        'subject': {},
        'education_category': {},
        'alternate_type': {},
        'geologic_time': {},
    },
    'M2M_REGISTRY': {
        'grade': {},
        'subject': {},
        'education_category': {},
        'alternate_type': {},
        'geologic_time': {},
    }
}

And your settings here will update those.

Relationship Definition

Each model uses a relationship definition to configure and relate itself to other models. The basic format is:

{'app.ModelName': 'fieldname'}

Which will create a foreign key on app.ModelName named fieldname and relates to the assigned model (Grade, Subject, etc.). There is a more advanced syntax that allows you to specify any keyword arguments that a Django ForeignKey field or ManyToManyField will accept.

{'app.ModelName': {'name': 'fieldname', 'other': 'kwargs'}}

The name key is required and is used for the field’s name. You can also define several relationships within the same model:

{'app.ModelName': ('fieldname1', {'name': 'fieldname2', 'other': 'kwargs'})}

Registering one metadata field to model

The simplest way is to specify the name of the field, such as:

EDUMETADATA_SETTINGS = {
    'FK_REGISTRY': {
        'grade': {'app.AModel': 'grade'}
    }
}

This is equivalent to adding the following the AModel of app:

grade = models.ForeignKey(Grade)

If you want more control over the new field, you can use a dictionary and pass other ForeignKey options. The name key is required:

EDUMETADATA_SETTINGS = {
    'FK_REGISTRY': {
        'grade': {
            'app.AModel': {
                'name': 'grade',
                'related_name': 'amodel_grades'
            }
        }
    }
}

This is equivalent to adding the following the AModel of app:

grade = models.ForeignKey(Grade, related_name='amodel_grades')

Registering two or more of the same metadata fields to a Model

When you want more than one relation to Grade, all but one of the fields must specify a related_name to avoid conflicts, like so:

EDUMETADATA_SETTINGS = {
    'FK_REGISTRY': {
        'grade':{
            'app.MyModel': (
                'primary_grade',
                {
                    'name': 'secondary_grade',
                    'related_name': 'mymodel_sec_grade'
                },
            )
        }
    },

Registering one or more Many-to-Many Grade fields to a Model

EDUMETADATA_SETTINGS = {
    'M2M_REGISTRY': {
        'app.AModel': 'grades',
        'app.MyModel': (
            {'name': 'other_grades', 'related_name': 'other_grades'},
            {'name': 'more_grades', 'related_name': 'more_grades'},
        ),
    }
}