Cassandra's Python CQLEngine model inheritance - python

I have the following object model:
class Data(Model):
__keyspace__ = 'varilog'
__table_name__ = 'md_data'
id = columns.TimeUUID(partition_key=True, primary_key=True, required=True)
device = columns.Text(primary_key=True, required=True)
property = columns.Text(primary_key=True, required=True)
field = columns.Text(primary_key=True, required=True)
cyclestamp = columns.DateTime(static=True)
type = columns.Text(discriminator_column=True)
#text_value = columns.Text() # Will work
#value = columns.Text(db_field='text_value') # Will work but...
class DataText(Data):
__discriminator_value__ = 'str'
value = columns.Text(db_field='text_value') # Always None
#text_value = columns.Text() # Ok also
When I query an object, depending on the value of the type column, the correct object is returned (DataText in this example), however it's value is None while if I uncomment text_value I'll have the correct value.
It looks like db_field is not supported in a child class. Is this a bug?

Related

How to set a fixed field in mongoengine/pymongo

I'd like to set for the ProofModel subclasses to have the type_ field set and immutable.
class ProofType(Enum):
JWS = "RsaVerificationKey2018"
HASH = "Sha2"
class ProofModel(EmbeddedDocument):
type_ = EnumField(ProofType, required=True)
created = DateTimeField(default=datetime.now(), required=True)
meta = {"allow_inheritance": True}
class JwsProofModel(ProofModel):
type_ = EnumField(ProofType, default=ProofType.JWS)
jws = StringField(required=True)
class ShaProofModel(ProofModel):
type_ = EnumField(ProofType, default=ProofType.HASH)
hash_ = StringField(required=True)
I could leave it as above but my need is to limit the ability to create a (for example) JwsProofModel having the type_ specified when instantiated
I see 2 options:
1)
(not released yet) You can abuse the choices parameter on the EnumField constructor to limit the allowed value to just 1 value.
class JwsProofModel(ProofModel):
type_ = EnumField(ProofType, default=ProofType.JWS, choices=[ProofType.JWS])
jws = StringField(required=True)
This feature is only available on the development version (future 0.23.2) but it should get released shortly
2)
As a workaround, use the custom per-field validation feature (doc)
def is_jws(value):
if value != ProofType.JWS:
raise ValidationError("wrong value")
class JwsProofModel(Document):
type_ = EnumField(ProofType, default=ProofType.JWS, validation=is_jws)

How do I write a default value to a Column where the default value is a value created from a function that uses another Column?

Make flask-app, that is used .docx file (home/user/WebApp/app/docxfiles/*.docx) and display them using sqlalchemy. Using MySQL. Columns are written from flask-admin. Here's a piece of code that you can't do right. How do I write a default value to a Сolumn where the default value is a value created from a function that uses another Сolumn?
class Item(db.Model):
def getinfo(namefile):
path_file = os.path.abspath(os.path.dirname(__file__)) + "/docxfiles/" + namefile
doc = docx.Document(path_file)
fulltext = []
for i in doc.paragraphs:
fulltext.append(i.text)
body = '\n\n'.join(fulltext)
return re.sub('<(.|\n)*?>','',body)
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
namefile = db.Column(db.String(200), unique=True)
info = db.Column(db.String(16777216), server_default = getinfo(namefile))
Column default values passed as server_default or default values are useful for setting fixed default values or date or timestamps but do not accept runtime arguments to allow more complex processing such as in your example. The correct place to put such code is in a constructor to your model class which will be called only on creation of a new object but not when an object is retrieved from the database. The only changes needed in your code to make this work is to turn the getinfo function into an __init__ method and to manually set the values of namefile and info.
class Item(db.Model):
def __init__(self, namefile):
path_file = os.path.abspath(os.path.dirname(__file__)) + "/docxfiles/" + namefile
doc = docx.Document(path_file)
fulltext = []
for i in doc.paragraphs:
fulltext.append(i.text)
body = '\n\n'.join(fulltext)
self.info = re.sub('<(.|\n)*?>','',body)
self.namefile = namefile
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
namefile = db.Column(db.String(200), unique=True)
info = db.Column(db.String(16777216))

Flask-Restless dump_to of primary key field

I am running into an issue that may be bug, but want to verify it with the community. I am basically trying to conform to camelcase for transporting data, then underscore for the database.
However, on the person_serializer, flask-restless will not allow an outbound "idPerson" as a result of the dump_to="idPerson". For some reason, it checks that the primary key exists and gets a keyError since the actual key is "id_person", not "idPerson".
Any help would be appreciated.
class Person(Base):
__tablename__ = "person"
id_person = Column(Integer, primary_key=True)
first_name = Column(String(50))
last_name = Column(String(50))
class PersonSchema(Schema):
id_person = fields.Integer(load_from="idPerson",dump_to="idPerson")
first_name = fields.String(load_from="firstName", dump_to="firstName")
last_name = fields.String(load_from="lastName", dump_to="lastName")
#post_load
def make_user(self, data):
return Person(**data)
person_schema = PersonSchema()
def person_serializer(instance):
return person_schema.dump(instance).data
def person_deserializer(data):
return person_schema.load(data).data
KEY ERROR IS BELOW
try:
# Convert the dictionary representation into an instance of the
# model.
instance = self.deserialize(data)
# Add the created model to the session.
self.session.add(instance)
self.session.commit()
# Get the dictionary representation of the new instance as it
# appears in the database.
result = self.serialize(instance)
except self.validation_exceptions as exception:
return self._handle_validation_exception(exception)
# Determine the value of the primary key for this instance and
# encode URL-encode it (in case it is a Unicode string).
pk_name = self.primary_key or primary_key_name(instance)
> primary_key = result[pk_name]
E KeyError: 'idPerson'

Odoo error when generating One2many field data

Given the following class for the header:
class vontatas_head(models.Model):
_name = 'vontatas.head'
display_name = fields.Char(string="Sor", compute='_compute_display_name', store=False)
plan_type_id = fields.Many2one(
comodel_name='plan.type', string='Terv típus', required=True)
year = fields.Integer(string='Év', required=True, default=lambda *a: strftime('%Y'))
version = fields.Integer(string='Verzió', required=True, default=1)
comment = fields.Char(string='Megjegyzés')
vontatas_data_ids = fields.One2many(
comodel_name='vontatas.data', inverse_name='vontatas_id', string='Adatok', default=get_default_lines)
And for the detail:
class vontatas_data(models.Model):
_name = 'vontatas.data'
vontatas_id = fields.Many2one(comodel_name="vontatas.head", string="Vontatás sor")
name = fields.Char(string="Megnevezés", required=True)
code = fields.Char(string="Kód", required=True)
type = fields.Selection([('total', 'Összesen'), ('input', 'Input')], string="Típus", default='input')
value = fields.Float(string="Várható költség")
parent_id = fields.Many2one(comodel_name="vontatas.data", ondelete='cascade', string="Összesen sor")
child_ids = fields.One2many(comodel_name="vontatas.data", inverse_name='parent_id', string='Input sorok')
I have to automatically generate details data from a template with this code:
def get_default_lines(self):
self.env.cr.execute("select name, code, type, parent_id from vontatas order by code")
sorok = self.env.cr.fetchall()
ids = []
for sor in sorok:
ids.append((0,0, { 'name': sor[0],
'code': sor[1],
'type': sor[2],
'parent_id': sor[3]
}))
return ids
Everyting is working fine, but at the creation I have an error message: "One of the documents you are trying to access has been deleted, please try again after refreshing."
I know why this error is happening: at the moment of generation there is no valid parent_id.
My question is: how to provide any valid parent_id within the function get_default_lines, knowing that the data is actually just in memory, not stored yet in the database?
Or asked otherwise: How to keep the hierarchy level defined within the template?

django - can't assign a foreign key

For unknown reasons, I cannot assign a foreign key instance of Item_rarity table into Detailed_item table. Django throws an error:
Cannot assign "u'Basic'": "Detailed_item.rarity" must be a "Item_rarity" instance.
... But in Item_rarity dictionary "Basic" record exists - I can choose it from admin panel and create Detailed_item record manually.
I have defined models:
class Detailed_item(models.Model):
item_id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=50)
level = models.IntegerField()
icon = models.CharField(max_length=150)
rarity = models.ForeignKey('Item_rarity')
general_type = models.ForeignKey('Item_type')
detailed_type = models.ForeignKey('Item_detailed_type')
class Item_rarity(models.Model):
name = models.CharField(max_length=15, primary_key=True)
class Item_type(models.Model):
name = models.CharField(max_length=15, primary_key=True)
class Item_detailed_type(models.Model):
name = models.CharField(max_length=20, primary_key=True)
In views, I try to populate it in this manner (inserting multiple items):
...
items = get_all_items() #get dict of items
for element in items:
tmp_det_type = ''
for key, val in element.iteritems():
#get 'detailed type' from inner dict
if key == "type":
tmp_det_type = val
item = Detailed_item(
item_id=element['id'],
name=element['name'],
level=element['level'],
icon=element['icon'],
rarity=element['rarity'], #error
general_type=element['type'],
detailed_type=tmp_det_type,
)
item.save()
...
I even tried to hard code "Basic" string, but it doesn't work either.
* Solved *
Next two entries, that is Item_type and Item_detailed_type were also invalid.
Correct code:
from app.models import Detailed_item, Item_rarity, Item_type, Item_detailed_type
...
items = get_all_items() #get dict of items
for element in items:
tmp_det_type = ''
for key, val in element.iteritems():
#get 'detailed type' from inner dict
if key == "type":
tmp_det_type = val
#create objects with string values
obj_rarity = Item_rarity(name=element['rarity'])
obj_item_type = Item_type(name=element['type'])
obj_item_detailed_type = Item_detailed_type(name=tmp_det_type)
item = Detailed_item(
item_id=element['id'],
name=element['name'],
level=element['level'],
icon=element['icon'],
rarity=obj_rarity,
general_type=obj_item_type,
detailed_type=obj_item_detailed_type,
)
item.save()
...
Item_rarity instance should be passed while storing Detailed_item object since Item_rarity is a foreign key related object in Detailed_item.
Its that you might have passed the Basic string instead of the <Basic Object> itself.
While creating an object in django using its ORM, any foreign_key related object should be provided with the instance itself instead of the id(pk) of the object, where as while fetching the data from the database you can use either of instance or the id(pk) of the instance.
class ParentModel(models.Model):
model_field = models.CharField(max_length=16)
class MyModel(models.Model):
some_field = models.ForeignKey('ParentModel')
parent_model = ParentModel.objects.create(model_field='some_data')
my_model = MyModel.objects.create(some_field=parent_model)
^^^^^^^^^^^^
Note here that the parent_model object itself is passed instead of the id
While fetching the data back,
parent_model = ParentModel.objects.get(model_field='some_data')
my_model = MyModel.objects.get(some_field=parent_model)
or
my_model = MyModel.objects.get(some_field=parent_model.id)
Both would work in case of data fetch.
You do not have to provide the related object on creation if you change the kwarg in to rarity_name:
item = Detailed_item(
item_id=element['id'],
name=element['name'],
level=element['level'],
icon=element['icon'],
rarity_name=element['rarity'], # no error
general_type=element['type'],
detailed_type=tmp_det_type,
)
I have only tested this with the regular id field (the auto pk) but it
should work with your primary key just fine.
E.g.
class SimpleModel(Model):
value = TextField(blank=True)
class ComplexModel(Model):
simple = ForeingKey(SimpleModel)
title = TextField(unique=True)
ComplexModel.objects.create(title='test', simple_id=1)

Categories

Resources