Closed ckoerber closed 5 years ago
This feature was added with commit dfbc07dc3f8128ba8c9985e984bbd750fe955d03.
See lattedb.base.models.get_or_create_from_parameters
.
Creates class and dependencies through top down approach from parameters. 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.
Example
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, instances = A.get_or_create_from_parameters(
parameters={"a": 1, "b1": 2, "b2": 3, "b3": 4, "c1": 5},
tree={"b1": "BA", "b2": "BB", "c": ("C", {"c2": "BA"})}
specialized_parameters={"b2": {"b2": 10}}
)
will get or create the instances
a = A.objects.all()[-1]
a == instance
a == instances[-1]
a.a == 1 # key of A from pars
a.b1.b1 == 2 # a.b1 is BA through tree and a.b1.b1 is two from pars
a.b1.b2 == 3
a.b2.b1 == 2 # a.b2 is BB through tree and a.b1.b1 is two from pars
a.b2.b2 == 10 # a.b2.b2 is overwriten by specialized_parameters
a.b2.b3 == 4
a.c.c1 == 5 # a.c is C through tree
a.c.c2.b1 == 2 # a.c.c2 is BA through tree
a.c.c2.b2 == 3
Integrate changes made to
nucleon_elastic_ff.quarkson.get_data
part oflattedb
models.