Relational field reference

This document describes the different types of relational fields, that are available in androrm. You can quick jump to any field reference using the respective link in the side menu.


ForeignKeyField<T extends Model> Since version 0.1

If you want to create a link between two of your model classes, you can do this with a ForeignKeyField. The type attribute of the field has to be set to the type of the class this field is referencing.

Androrm will automatically create a database field for you, that links to the respective table for the other model class. No need for you, to create this manually.

ForeignKeyField<T> ( Class<T> targetRef )

This constructor will initialize the field and create all relevant information.

targetRef

The class of the model, this relation is referencing.


void doNotCascade ( )

By default all ForeignKeyFields are initialized with the ON DELETE CASCADE option. This means, if the model, this field references is deleted, then also all referencing instances will be deleted from the database. If you want to suppress this behavior, call this method right after initializing the field.


T get ( Context context )

Retrieves the linked model from the database. Note that this function is lazy, meaning that the database query will only take place, the first time you use this function.

context

The context your android application is executed in. It is good practice here to hand in a Context instance, that you can get via calling getApplicationContext() inside your activity.


void set ( T value )

Sets up the field to reference the model instance you reference with modelRef. Note, that this will only be persisted to the database, if you call save at some point after this. Also, the model, that is being referenced has to be persistent in the database.

value

An instance of a model class of type T.


ManyToManyField<O extends Model, T extends Model> Since version 0.1

If you want to express a relation between two model classes and a simple ForeignKey constraint is not enough, maybe a ManyToMany relation fits your needs. In general this is always the case, if you want to reference several instances of the target class from the origin class and the other way round.

Again, you only have to do the field definition on your classes. The rest is done by androrm. Behind the scenes it will create a relation table and take care of all references.

ManyToManyField<O, T> ( Class<O> originRef, Class<T> targetRef )

To give androrm the information about the origin and target classes, that it needs in order to set up the relation table, you have to call this constructor and hand in the class objects of you models. In order, to provide you with the correct functions to set and get data from the field, also set the correct type parameters here.

originRef

The class of the model, that is referencing another model. Normally, that should be the class, this field is defined in.

targetRef

The class of the model, this relation is referencing.


void add ( T value )

Adds a new value to the list of values, that are a associated with this model instance.

value

An instance of a model class of type T.


void addAll ( Collection<T> values )

Takes all values in the collection and adds them to the value list. Note that any type of list or set, that implements the Collection interface is accepted. Popular implementors are HashSet or ArrayList.

values

A Collection of values of type T.


boolean contains ( T value )

Checks whether the given value is contained in the result. Note, that this function checks against the database. So, newly added values will only be recognized, if save() has been called on the object.

value

An instance of a model class of type T.


boolean containsAll ( Collection<T> values )

Checks whether all given values are present in the result. Returns true only if all values are contained. Note, that this method checks against the database. So newly added values will only be recognized, if save() has been called on the model instance.

values

A Collection of values of type T.


QuerySet<T> get ( Context context, O originRef )

This method will retrieve a query set object from the field with a set up query, that will by default return all objects this field points to. As any other query set, further restrictions as filters can be applied.

context

The context your android application is executed in. It is good practice here to hand in a Context instance, that you can get via calling getApplicationContext() inside your activity.

originRef

Instance of the model class, that is referencing the other model. Normally you will hand in this.


OneToManyField<O extends Model, T extends Model> Since version 0.1

Sometimes you have the situation, that a lot of instances of one class, reference only one instance of another class. This is normally done through ForeignKey fields on these classes. If you now want to traverse this relation backwards, from the references class, then this relation field should be your choice.

It does not create any new fields, but analyzes the target class for a back reference. You might have already recognized it. In order for this field to work you have to create a ForeignKey in the targeted class.

OneToManyField<O, T> ( Class<O> originRef, Class<T> targetRef )

As androrm can't gather enough information from the type parameters, you need to specify the class objects of both the target and the origin class of this relation.

originRef

The class of the model, that is referencing another model. Normally, that should be the class, this field is defined in.

targetRef

The class of the model, this relation is referencing.


void add ( T value )

Adds a new value to the list of values, that are a associated with this model instance.

value

An instance of a model class of type T.


void addAll ( Collection<T> values )

Takes all values in the collection and adds them to the value list. Note that any type of list or set, that implements the Collection interface is accepted. Popular implementors are HashSet or ArrayList.

values

A Collection of values of type T.


boolean contains ( T value )

Checks whether the given value is contained in the result. Note, that this function checks against the database. So, newly added values will only be recognized, if save() has been called on the object.

value

An instance of a model class of type T.


boolean containsAll ( Collection<T> values )

Checks whether all given values are present in the result. Returns true only if all values are contained. Note, that this method checks against the database. So newly added values will only be recognized, if save() has been called on the model instance.

values

A Collection of values of type T.


QuerySet<T> get ( Context context, O originRef )

This method will retrieve a query set object from the field with a set up query, that will by default return all objects this field points to. As any other query set, further restrictions as filters can be applied.

context

The context your android application is executed in. It is good practice here to hand in a Context instance, that you can get via calling getApplicationContext() inside your activity.

originRef

Instance of the model class, that is referencing the other model. Normally you will hand in this.

Contents

androrm on github

Django is a registered trademark of the Django Foundation. Android is a registered trademark of Google Inc.