Problems overwriting a model field in Python in Odoo? - python

I have created a module which modifies other one (named base). In the module base there is the res.partner model, and in this model there is the field birthdate:
_columns = {
...
'birthdate': fields.char('Birthdate'),
...
}
What I do in my module is to overwrite this field to make it of type Date:
birthdate = fields.Date('Birthdate')
Everything seems OK, but, after updating the Odoo server, the data introduced in that column dissapears from the view, and when I check the database, I find that the column birthdate is being duplicated with other names like birthdate_moved0, birthdate_moved1, birthdate_moved2, etc... (and half of them are of type char and the other half of type date). The values stored in birthdate are being moved to these other columns (that's the reason bacause I can't see the data in the view, since in the form only birthdate is being shown).
However, I was able to overwrite several fields through Python. But this duplication problem happened me with the field birthdate and the field function of the model res.partner.
I can't come to a conclussion. Can anyone help me here, please? Thank you in advance!

You should name your "new" field 'birth_date' or 'dob' or anything other than 'birthday' just to avoid changing existing field data type. In next step you can copy values from current 'birthday' field to new one (through postgresql).

Finally, a co-worker shown me the solution:
It's not about Odoo, it's due to PostgreSQL. Generally, in PostgreSQL, is not possible to alter the data type of a column (even when this is empty), except for some cases, like for example:
From integer to char: because the casting to char is possible. Therefore, in Odoo, when you change the data type of a field.Date, a field.Integer, a field.Many2one, etc... to a fields.Char, there is no problem. However, if you try to change a fields.Char to fields.Date or fields.Many2one, or whatever, PostgreSQL is going to duplicate the column because is not ready for that type of casting.
That's the reason because I wasn't able to change a field of type Char and transform it in a field of kind Date (my attempt with birthdate) or Many2one (my attempt with function). And on the other hand, I was able to overwrite a Selection field (actually, in PostgreSQL is convert a Char into another Char).
So in conclusion:
If you are going to change the type of data of a field, check if the final kind of data is char (fields.Char, fields.Selection, etc...) or other possible casting. Then you can name the new field with the same name as before. If not, you must name the new field with other name, otherwise PostgreSQL will duplicate the column with name_moved0, name_moved1, etc...
I hope this helps to anyone!!

Related

MongoDB Python: Unable to remove a field that has a . in it

I'm using pymongo and I'm unable to remove a field that has a "." in the field name. I've tested this same code with other fields that do not have a period and it works without an issue.
db.city.update_many({}, {'$unset': {'AverageDailyPM2.5':1}})
I'm pretty certain I'm on the latest versions of everything including MongoDB 4.4.9.
That is one of the reasons it is not recommended to use "." in a field name.
If you use the pipeline form of update, you can convert the object to an array with $objectToArray so the fields become data, remove the desired field from the array with $filter, convert back to an object with $arrayToObject, and then use $replaceRoot to make that the final document.

HP Quality Center field names

I am interacting with HP QC using python and referring to HP ALM OTA documentation.
What I need is to access fields in different places (particularly now I am trying to access Test Set description field). As far as I know it is done by following: TestSet['description field name'] = 'I am description' The problem is - I don't know this field name and I can't find it in documentation mentioned.Up until now I was wondering around examples in hope to find these names (the way I found that Actual field in test step is named 'ST_ACTUAL').
Could you please help me find some kind of list of these field names. Or the way to retrieve them.. (Or at least give me the name of this Test Set description field)
When you get an entity field value, the field must be the underlying database column name for that entity. You can discover this using the project customization UI in HP ALM: select project entities then explore the system or user fields. Beware that the Design Step says the column name begins ST_... it doesn't. It's actually DS_...
You can also get this information programmatically. Given a factory instance use the equivalent of:
private void ExploreFactoryFieldDefinitions(IBaseFactory factory)
{
List fields = factory.Fields;
foreach (TDField field in fields)
{
FieldProperty field_property = (FieldProperty)field.Property;
if (field_property.IsRequired)
{
Log(String.Format("User Label: {0}\n", field_property.UserLabel));
Log(String.Format("User Column Type: {0}\n", field_property.UserColumnType));
Log(String.Format("DB Column Name: {0}\n", field_property.DBColumnName));
Log(String.Format("DB Column Type: {0}\n", field_property.DBColumnType));
Log(String.Format("DB Table Name: {0}\n", field_property.DBTableName));
}
}
}
field_property.UserLabel gives you the user friendly field name. field_property.DBColumn name gives you the database column name that should be used with entity[field_name].
BTW - don't forget to call entity.Post() to have your changes saved. When working with a versioned project you have a few more hoops to jump through too. Good luck!
I think, the field you are looking for is CY_COMMENT (hint). Maybe there is a better way—but you can find the names of the fields in the Query Builder. If you create an Excel Report and open the Query Builder, there is an Entities View which shows all the fields of the tables (even the user-defined fields). Maybe there is some kind of database documentation which gives you the same thing.

Using Flask's MongoEngine. Error with datetime field

I have a model with the a datetime field
user_since = db.DateTimeField()
When I try to insert a new object of the model into mongo, There is no error. But the write does not succeed.
I printed the object from to_json() and tried to insert it with mongo shell, I get the following error.
field names cannot start with $ [$date] at src/mongo/shell/collection.js:L147
the to_json had this field.
"user_since": {"$date": 1392205572989}
I can't seem to find any pointers on how to solve this.
What is causing the write to fail?
How can I make mongoengine to throw error in case of write failure.? Or at least find out what the error was?
Thanks.
Update:
As I found later the real problem is not the datetime field. The details of the problem are in this question MongoEngine Document Object made using from_json doesn't save
With MongoDB you cannot have '$' at the beginning of the field name.
MongoDB Limits and Thresholds - Restrictions on Field Names:
Field names cannot contain dots (i.e. .), dollar signs (i.e. $), or
null characters. See Dollar Sign Operator Escaping for an alternate
approach.
Try to name it differently.
edit:
According to MongoEngine documentation, you could pass db_name parameter:
db_field – The database field to store this field in (defaults to the
name of the field)

Getting key from the id of the entity using Appengine's NDB

I have a model based on ndb, while saving it, I stored 'id' field with current logged-in user's user id. (Why I am doing this? Actually this model used to be based on db.Model and key_name has this user's id. Now, I am converting it to ndb)
m= Modelclass(id = str(users.get_current_user().used_id()),
--- Other fields ---
m.put()
This model's edit form sends this 'id' and I wanted to get corresponding 'key' from it. But, I got "Key id number is too long; received 'some big_number'". Tried both ways
Modelclass.get_by_id(<id>).key
OR
ndb.Key('Modelclass', <id>)
This is one case, there may be other cases where user can store some big number in 'id' field. In these scenarios, we can't extract key from 'id'. So, how to solve such a problem.
I am new to ndb. Thanks for any help.
Looks like your value is an int, not a string. But you converted it into a string when creating the entitiy. There's a simple solution:
ndb.Key('Modelclass', str(<id>))
Good luck!

unique python/django issue with variable column/field name in form

I have quite unique problem with django.
Im providing website users interface for editing large data. Each row on this data represents a row in database. Or one object of certain Type.
Users click on cells in the table and form opens where they can edit this fields/column value.
In essence it works like this:
1) based on where user clicks, query is sent to server containting object id and the field that he is editing.
2) based on this information form is created on the fly:
class FieldEditorForm(forms.ModelForm):
class Meta:
model = MyObject
fields = ['id', field ]
Notice the field there is Variable not name of the field.
3) this field passes its own modelform validation and all is fine. in save method Model.save() is enough to update the value.
But now to the problem. Sometimes empty value is sent to server in this form. Empy value such as u'' or almost emtpty like u' '. I want to repace this with None so NULL would be saved to database.
There are two places where i could do that. In field validation modifying the cleaned_data or in form save method.
Both approaches raise unique problem as i dont know how to create variable function names.
def clean_%(field)s():
or in case of form save method
r.%(field)s = None
is what i need, but those methods dont work. So how can i create method name which is variable or set objects variable parameter to something. Is it even possible or do i have to rethink my approach there?
Alan
In the latter case, setattr(r, field + 's', None).

Categories