aiida.orm.implementation.django package

Submodules

Classes and methods for Django specific backend entities

class aiida.orm.implementation.django.entities.DjangoModelEntity(*args, **kwargs)[source]

Bases: typing.Generic

A mixin that adds some common Django backend entity methods

MODEL_CLASS = None
__dict__ = mappingproxy({'__module__': 'aiida.orm.implementation.django.entities', '__doc__': 'A mixin that adds some common Django backend entity methods', 'MODEL_CLASS': None, '_dbmodel': None, '_auto_flush': (), '_class_check': <classmethod object>, 'from_dbmodel': <classmethod object>, 'get_dbmodel_attribute_name': <classmethod object>, '__init__': <function DjangoModelEntity.__init__>, 'dbmodel': <property object>, 'id': <property object>, 'pk': <property object>, 'is_stored': <property object>, 'store': <function DjangoModelEntity.store>, '__orig_bases__': (typing.Generic[~ModelType],), '__dict__': <attribute '__dict__' of 'DjangoModelEntity' objects>, '__weakref__': <attribute '__weakref__' of 'DjangoModelEntity' objects>, '__parameters__': (~ModelType,)})
__init__(*args, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'aiida.orm.implementation.django.entities'
__orig_bases__ = (typing.Generic[~ModelType],)
__parameters__ = (~ModelType,)
__weakref__

list of weak references to the object (if defined)

_auto_flush = ()
classmethod _class_check()[source]

Assert that the class is correctly configured

_dbmodel = None
dbmodel
classmethod from_dbmodel(dbmodel, backend)[source]

Create a DjangoEntity from the corresponding db model class

Parameters:
  • dbmodel – the model to create the entity from
  • backend – the corresponding backend
Returns:

the Django entity

classmethod get_dbmodel_attribute_name(attr_name)[source]

Given the name of an attribute of the entity class give the corresponding name of the attribute in the db model. It if doesn’t exit this raises a ValueError

Parameters:attr_name
Returns:the dbmodel attribute name
Return type:str
id
is_stored

Is this entity stored?

Returns:True if stored, False otherwise
pk

Get the principal key for this entry

Returns:the principal key
store()[source]

Store the entity

Returns:the entity itself

Utilities for the implementation of the Django backend.

class aiida.orm.implementation.django.utils.ModelWrapper(model, auto_flush=())[source]

Bases: object

Wrap a database model instance to correctly update and flush the data model when getting or setting a field.

If the model is not stored, the behavior of the get and set attributes is unaltered. However, if the model is stored, which is to say, it has a primary key, the getattr and setattr are modified as follows:

  • getattr: if the item corresponds to a mutable model field, the model instance is refreshed first
  • setattr: if the item corresponds to a mutable model field, changes are flushed after performing the change
__dict__ = mappingproxy({'__module__': 'aiida.orm.implementation.django.utils', '__doc__': 'Wrap a database model instance to correctly update and flush the data model when getting or setting a field.\n\n If the model is not stored, the behavior of the get and set attributes is unaltered. However, if the model is\n stored, which is to say, it has a primary key, the `getattr` and `setattr` are modified as follows:\n\n * `getattr`: if the item corresponds to a mutable model field, the model instance is refreshed first\n * `setattr`: if the item corresponds to a mutable model field, changes are flushed after performing the change\n ', '__init__': <function ModelWrapper.__init__>, '__getattr__': <function ModelWrapper.__getattr__>, '__setattr__': <function ModelWrapper.__setattr__>, 'is_saved': <function ModelWrapper.is_saved>, 'save': <function ModelWrapper.save>, '_is_mutable_model_field': <function ModelWrapper._is_mutable_model_field>, '_is_model_field': <function ModelWrapper._is_model_field>, '_flush': <function ModelWrapper._flush>, '_ensure_model_uptodate': <function ModelWrapper._ensure_model_uptodate>, '_in_transaction': <staticmethod object>, '__dict__': <attribute '__dict__' of 'ModelWrapper' objects>, '__weakref__': <attribute '__weakref__' of 'ModelWrapper' objects>})
__getattr__(item)[source]

Get an attribute of the model instance.

If the model is saved in the database, the item corresponds to a mutable model field and the current scope is not in an open database connection, then the field’s value is first refreshed from the database.

Parameters:item – the name of the model field
Returns:the value of the model’s attribute
__init__(model, auto_flush=())[source]

Construct the ModelWrapper.

Parameters:
  • model – the database model instance to wrap
  • auto_flush – an optional tuple of database model fields that are always to be flushed, in addition to the field that corresponds to the attribute being set through __setattr__.
__module__ = 'aiida.orm.implementation.django.utils'
__setattr__(key, value)[source]

Set the attribute on the model instance.

If the field being set is a mutable model field and the model is saved, the changes are flushed.

Parameters:
  • key – the name of the model field
  • value – the value to set
__weakref__

list of weak references to the object (if defined)

_ensure_model_uptodate(fields=None)[source]

Refresh all fields of the wrapped model instance by fetching the current state of the database instance.

Parameters:fields – optionally refresh only these fields, if None all fields are refreshed.
_flush(fields=None)[source]

Flush the fields of the model to the database.

Note

If the wrapped model is not actually save in the database yet, this method is a no-op.

Parameters:fields – the model fields whose currently value to flush to the database
static _in_transaction()[source]

Return whether the current scope is within an open database transaction.

Returns:boolean, True if currently in open transaction, False otherwise.
_is_model_field(name)[source]

Return whether the field is a field of the model.

Returns:boolean, True if the field is a model field, False otherwise.
_is_mutable_model_field(field)[source]

Return whether the field is a mutable field of the model.

Returns:boolean, True if the field is a model field and is not in the IMMUTABLE_MODEL_FIELDS set.
is_saved()[source]

Retun whether the wrapped model instance is saved in the database.

Returns:boolean, True if the model is saved in the database, False otherwise
save()[source]

Store the model instance.

Raises:aiida.common.IntegrityError – if a database integrity error is raised during the save.