Hi,
Here's my version of Benjamin's solution for ODOO 10 :
xml :
< odoo>
< data>
< function model="clean.field" name="init_remove_field_api7" eval="['account.indicateur.custom.ltq','total_achats']" / >
< /data>
< /odoo>
py file :
import logging
from odoo import models, fields, api, _, SUPERUSER_ID, tools
from odoo.models import TransientModel
_logger = logging.getLogger(__name__)
class CleanField(TransientModel):
""" Classe pour supprimer un champ de la BDD."""
_name = "clean.field"
@api.model
def init_remove_field_api7(self, model_name="", field_name=""):
"""Entry function remove Field (called with API7)."""
if model_name and field_name and type(model_name) == str and type(field_name) == str:
table_name = model_name.replace(".", "_")
_logger.info(u"init_remove_field_api7 : removing database field {}.{}".format(table_name, field_name))
return self._remove_field(model_name, field_name)
else:
return False
@api.model
def _remove_field(self, model_name="", field_name="", context=None):
"""Removes the Field from the database."""
query1 = """SELECT 1 FROM ir_model_fields WHERE model = '{}' AND name='{}';""".format(model_name or "", field_name or "")
self.env.cr.execute(query1)
fields = self.env.cr.fetchall()
if fields:
table_name = model_name.replace(".","_")
query2 = """DELETE FROM ir_model_fields WHERE model = '{}' AND name = '{}';
ALTER TABLE {} DROP COLUMN {};""".format(model_name, field_name, table_name, field_name)
_logger.info(u"_remove_field : query = {}".format(query2 or "None"))
self.env.cr.execute(query2)
self.env.cr.commit()
return True
Perhaps another alternative is to hide the field from your view? If its purely for not wanting to show fields anymore this is an ideal solution.
Its good to hide the fields, and not considering into any calculation if you fear the data!
this solution would be fine, but if in the fields are sensible data like bank account numbers i do not want to just remove the view and the field. I want them deleted, so nobody can access them. And on the other way then i just remove the field my database stays big and will get bigger and bigger with a lot of junk data
It is simply not possible to delete any fields without uninstall. For example if we delete from database, then once your py file gets compiled again those fields will be created or if you not restart your Odoo, you will face an error. Better to make them invisible (this is the only way to deal with your problem) via editing views ( form, tree, kanban etc...) From fields screen it is not possible to inactivate them because those may be exist in views. There is one module exists with it we can persist the data when you uninstall the module. You can try it out.
@Benjamin, why you not delete the data by postgreSQL query, for the table on which there is sensitive data and then disable that field from view ?
I think this is the only solution to write a script wich will delete all the fields on a new server start. I hoped there may be a odoo function or feature i did not know about.
It's not solution, I wish your database is not in live usage when you go for this "lethal" experiment ;)
Backups will be the keyword here. :) Make sure that if you script something that it first takes a dump from your db before you start dropping records. Better save than sorry!