models

Module: espressodb.base.models

Base.get_or_create_from_parameters(…[, …])

Creates class and dependencies through top down approach from parameters.

Base.save(*args[, save_instance_only])

Overwrites user with login info if not specified and runs consistency checks.

Base.check_consistency()

Method is called before save.

Base.specialization

Returns the specialization of the instance (children with the same id).


This module provides the Base class which is an abstract model basis providing default interfaces for espressodb.

class Base(*args, **kwargs)[source]

The base class for the espressodb module.

This class provides api for auto rendering pages and recursive insertions.

__init__(*args, **kwargs)[source]

Default init but adds specialization attributes (which do not clash) to self

The specialization is a child instance of this class which has an id in the respective child table.

The specialization attributes are attributes present in the child but not in the current instance.

__setattr__(key, value)[source]

Tries to set the attribute in specialization if it is a specialized attribute and else sets it in parent class.

__str__()[source]

Verbose description of instance name, parent and column values.

Return type

str

check_consistency()[source]

Method is called before save.

Raise errors here if the model must fulfill checks.

check_m2m_consistency(instances, column=None)[source]

Method is called before adding to a many to many set.

Raise errors here if the adding must fulfill checks.

clean()[source]

Calls super clean, check_consistency.

Consistency errors are wrapped as validation errors. This ensures consistencies are properly captured in form validations and do not raise errors berfore.

classmethod get_app()[source]

Returns the name of the current moule app

Return type

AppConfig

classmethod get_app_doc_url()[source]

Returns the url tp the doc page of the app.

Return type

Optional[str]

Returns

Url if look up exist else None.

classmethod get_app_name()[source]

Returns the name of the current moule app

Return type

str

classmethod get_doc_url()[source]

Returns the url to the doc page.

Return type

Optional[str]

Returns

Url if look up exist else None.

classmethod get_label()[source]

Returns descriptive string about class

Return type

str

classmethod get_open_fields()[source]

Returns list of fields for class which are editable and non-ForeignKeys.

Return type

List[Field]

classmethod get_or_create_from_parameters(calling_cls, parameters, tree=None, dry_run=False, _class_name=None, _recursion_level=0)[source]

Creates class and dependencies through top down approach from parameters.

Parameters
  • calling_cls – The top class which starts the get or create chain.

  • parameters (Dict[str, Any]) – The construction / query arguments. These parameters are shared among all constructions.

  • tree (Optional[Dict[str, Any]]) – The tree of ForeignKey dependencies. This specify which class the ForeignKey will take since only the base class is linked against. Keys are strings corresponding to model fields, values are either strings corresponding to classes

  • dry_run (bool) – Do not insert in database.

  • _class_name (Optional[str]) – This key is used internaly to identified the specialization of the base object.

  • _recursion_level (int) – This key is used internaly to track number of recursions.

Populates columns from parameters and recursevily creates foreign keys need for construction. Foreign keys must be specified by the tree in order to instanciate the right tables. In case some tables have shared column names but want to use differnt values, use the specialized_parameters argument. This routine does not populate many to many keys.

Example

Below you can find an example how this method works.

class BA(BaseB):
    b1 = IntegerField()
    b2 = IntegerField()

class BB(BaseB):
    b1 = IntegerField()
    b2 = IntegerField()
    b3 = IntegerField()

class C(BaseC):
    c1 = IntegerField()
    c2 = ForeignKey(BaseB)

class A(BaseA):
    a = IntegerField()
    b1 = ForeignKey(BaseB)
    b2 = ForeignKey(BaseB)
    c = ForeignKey(BaseC)

instance, created = A.get_or_create_from_parameters(
    parameters={"a": 1, "b1": 2, "b2": 3, "b3": 4, "c1": 5, "b2.b2": 10},
    tree={
        "b1": "BA",
        "b2": "BB",
        "c": "C",
        "c.c2": "BA"
    }
)

will get or create the instances

a0 = A.objects.all()[-1]
a0 == instance

a0.a == 1        # key of A from pars
a0.b1.b1 == 2    # a.b1 is BA through tree and a.b1.b1 is two from pars
a0.b1.b2 == 3
a0.b2.b1 == 2    # a.b2 is BB through tree and a.b1.b1 is two from pars
a0.b2.b2 == 10   # a.b2.b2 is overwriten by specialized parameters
a0.b2.b3 == 4
a0.c.c1 == 5     # a.c is C through tree
a0.c.c2.b1 == 2  # a.c.c2 is BA through tree
a0.c.c2.b2 == 3
Return type

Tuple[Base, bool]

classmethod get_recursive_columns(tree=None, _class_name=None)[source]

Recursively parses table including foreign keys to extract all column names.

Parameters
  • tree (Optional[Dict[str, Any]]) – The tree of ForeignKey dependencies. This specify which class the ForeignKey will take since only the base class is linked against. Keys are strings corresponding to model fields, values are either strings corresponding to classes

  • _class_name (Optional[str]) – This key is used internaly to identified the specialization of the base object.

Return type

Tuple[Dict[str, List[str]]]

classmethod get_slug()[source]

Returns import path as slug name

Return type

str

get_specialization()[source]

Queries the dependency tree and returns the most specialized instance of the table.

Return type

Base

classmethod get_sub_info(root_key, tree)[source]

Extracts the class name and sub tree for a given tree and key

Parameters
  • root_key (str) – The key to look up in the dictionary

  • tree (Dict[str, Any]) – The tree of ForeignKey dependencies. This specify which class the ForeignKey will take since only the base class is linked against. Keys are strings corresponding to model fields, values are either strings corresponding to classes

Raises
  • TypeError – If the values of the dictionary are not of type string or Tuple

  • KeyError – If the key was not found in the dictionary

Return type

Tuple[str, Optional[Dict[str, Any]]]

id

Primary key for the base class

last_modified

Date the class was last modified

pre_save()[source]

Method is called before save and before check consistency.

This method can be used to overwrite custom column values. It has access to all information present at the .save() call.

save(*args, save_instance_only=False, **kwargs)[source]

Overwrites user with login info if not specified and runs consistency checks.

Parameters

save_instance_only (bool) – If true, only saves columns of the instance and not associated specialized columns.

Note

The keyword save_instance_only and check_consistency is not present in standard Django.

Return type

Base

property specialization

Returns the specialization of the instance (children with the same id).

If the class has no children which match the id, this will be the same object.

Return type

Base

tag

User defined tag for easy searches

property type

Returns the table type

Return type

Base

user

User who updated this object. Set on save by connection to database. Anonymous if not found.