Odoo 12: Many2one ondelete message? - odoo

Is it possible to change(edit) default ondelete message in Many2one field?
My field is:
parent_id = fields.Many2one("pgp.organizational.classifications", string="Parent classification", select=True, ondelete='restrict')
Default message is like this, but I won't to add my message:
"Odoo Server Error - Greška kod provjere
The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set
[objekt s referencom: pgp.organizational.classifications - pgp.organizational.classifications] "

You cannot change it in the Many2one field's declaration.
Code which generates this message is there: https://github.com/odoo/odoo/blob/12.0/odoo/service/model.py#L120-L154
Seems to be tricky to overload

Restricting and cascading deletes are the two most common options. RESTRICT prevents deletion of a referenced row. NO ACTION means that if any referencing rows still exist when the constraint is checked, an error is raised; this is the default behavior if you do not specify anything. (The essential difference between these two choices is that NO ACTION allows the check to be deferred until later in the transaction, whereas RESTRICT does not.) CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well. There are two other options: SET NULL and SET DEFAULT. These cause the referencing columns to be set to nulls or default values, respectively, when the referenced row is deleted. Note that these do not excuse you from observing any constraints. For example, if an action specifies SET DEFAULT but the default value would not satisfy the foreign key, the operation will fail.

I solved it by overloading unlink method.
Here is the code if it helps someone:
> #api.multi
> def unlink(self):
> for field in self:
> if field.parent_id:
> raise UserError(_('It is not possible to delete a record that is already used in transactions!'))
> return super(YourClass, self).unlink()

Related

Difference between update and update_attributes

In Rails 5, what is the difference between update and update_attributes methods. I'm seeing the following results for both the methods
Returns true/false
Checking for active record validation
Call backs are triggered
and also regarding update method a new thing was introduced in active record relation. I'm not able to understand it. What is the difference?
Moreover are we using update_attributes in Rails 5. It's not there in active record documentation.
I'm confused with all update methods. I need clarity
As of Rails 4.0.2, #update returns false if the update failed. Before Rails 4.0.2, #update returned the object that got updated. The main difference therefore was the return value. After this change, #update_attributes is just an alias of #update. It seems there are talks to deprecate #update_attributes in Rails 6 which is not released yet.
https://github.com/rails/rails/pull/31998
https://github.com/rails/rails/commit/5645149d3a27054450bd1130ff5715504638a5f5
From the rails 5 files it seems to me update can be used to update multiple objects(array of records) but update_attributes only work on single records otherwise both are same
From rails core files for update_attributes:
Updates a single attribute and saves the record.
This is especially useful for boolean flags on existing records. Also note that
Validation is skipped.
\Callbacks are invoked.
updated_at/updated_on column is updated if that column is available.
Updates all the attributes that are dirty in this object.
This method raises an ActiveRecord::ActiveRecordError if the
attribute is marked as readonly.
def update_attribute(name, value)
name = name.to_s
verify_readonly_attribute(name)
public_send("#{name}=", value)
save(validate: false)
end
For Update
Updates an object (or multiple objects) and saves it to the database, if validations pass.
The resulting object is returned whether the object was saved successfully to the database or not.
==== Parameters
+id+ - This should be the id or an array of ids to be updated.
+attributes+ - This should be a hash of attributes or an array of hashes.
==== Examples
# Updates one record
Person.update(15, user_name: "Samuel", group: "expert")
# Updates multiple records
people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
Person.update(people.keys, people.values)
# Updates multiple records from the result of a relation
people = Person.where(group: "expert")
people.update(group: "masters")
Note: Updating a large number of records will run an UPDATE
query for each record, which may cause a performance issue.
When running callbacks is not needed for each record update,
it is preferred to use {update_all}[rdoc-ref:Relation#update_all]
for updating all records in a single query.
def update(id, attributes)
if id.is_a?(Array)
id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
object.update(attributes[idx])
}
else
if ActiveRecord::Base === id
raise ArgumentError,
"You are passing an instance of ActiveRecord::Base to `update`. " \
"Please pass the id of the object by calling `.id`."
end
object = find(id)
object.update(attributes)
object
end
end
When we are working with update_column that time update is done on the database level there is no any contact with the rails ORM so whatever logic we have implemented like callbacks and validations all will be waste and wont be useful as this is going to be bypassed.
I found this article explained really well in just 30 seconds.
.update
Use update when you want to return false, for example in an if/else:
if record.update(params)
display_success
else
react_to_problem
end
.update!
Use update! when you want an error (for example: to avoid erroring silently, which could be very bad if an error was unexpected and you needed to know about it to fix it!):
record.update!(params) # raises is invalid
'update' respects the validation rules on model, while 'update_attributes' ignores validations.

Removing children entries from parent causes error on SaveChanges

I am getting the following error:
The association between entity types 'Docket' and 'DocketLine' has been severed but the foreign key for this relationship cannot be set to null. If the dependent entity should be deleted, then setup the relationship to use cascade deletes.
The issue comes about because I have a Docket (header) than has multiple children (DocketLines), and I am doing an update where I am adding new Lines to the docket header, and I am just adding those new DocketLines to the Docket.DocketLines collection (which works fine). But when I attempt to remove a DocketLine from the same collection using Docket.DocketLines.Remove(deletedLine), then this generates the error message above. Any idea why?
I had to change my code to remove the Lines directly from the _context.DocketLines.Remove/RemoveRange(...) collection in the end, and this works, but it seems odd that I would add new items to a child collection to insert new DocketLines, but couldn't remove items from that same collection to remove DocketLines.
Inside the DbContext/OnModelCreating method, comment the OnDelete behaviour of the entity that is giving you that problem:
That will allow you to avoid that problem.

Remove SQL constraint in OpenERP7

In OpenERP7, the core module account has a declaration for account.invoice which has, at some point, the following declaration:
addons/account/account_invoice.py:343
_sql_constraints = [
('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number must be unique per Company!'),
]
In a module which redefined account.invoice I wanted to remove the constraint with two different approaches:
Removing it in init (account_invoice::init(self, pool, cr))
def __init__(self, pool, cr):
super(account_invoice, self).__init__(pool, cr)
try:
cr.execute('ALTER TABLE account_invoice DROP CONSTRAINT IF EXISTS account_invoice_number_uniq')
finally:
pass
Replacing the constraint
_sql_constraints = [
('number_uniq', 'check(1=1)', 'Dummy check, always true, used to replace the previous constraint'),
]
However, when I reinstall the module in which those two declarations were made, I get an error (in the PG logs) telling me that the constraint account_invoice_number_uniq could not be craeted for a unique key since there's repeated data.
How can I prevent having such error? How can I prevent the system attempting to create (first; then... replace/delete) the constraint?
Check the Below Reference Link to remove the SQL Constraint Of Parent Class in Odoo
Click To See the Reference For Remove the SQL Constraint In Odoo(formally OpenERP)

web2py SQLFORM.grid () get field values on process

on my edit/add page for SQLFORM.grid I would like to get the values of some fields on process. In case this value in combination with another field (userid) already exists user has to be notified about it.
any suggestions?
You can simply define the validator for one of the fields so it doesn't allow duplicates when the other field is also a duplicate, and then let the standard form validation process handle everything:
db.define_table('mytable',
Field('userid', 'reference auth_user'),
Field('otherfield',
requires=IS_NOT_IN_DB(db(db.mytable.userid == request.vars.userid),
'mytable.otherfield')))
Whenever a form is submitted, the IS_NOT_IN_DB validator will return an error if the value of "otherfield" is duplicated among the set of records where "userid" is also duplicated.

Can I delete an entity that is not in cache?

I want to delete a record from the DB that hasn't been retrieved from a breeze query. The entity hasn't been retrieved so it's not in the cache, but I know the KEY of the record from another operation. Here's what I've tried:
create a new entity from the manager:
manager.createEntity(entityNames.book);
setting the ID :
bookToDelete().bookID(1); // bookToDelete is a ko observable from step 1
updating the state:
bookToDelete().entityAspect.setDeleted();
When I save changes, this transaction is not included in the JSON
You almost have it. Calling entityAspect.setDeleted on an 'Added' entity moves it directly to a 'Detached' state, which in effect removes it from the EntityManager, and hence it cannot be saved. This is deliberate. It handles the case where you create an entity and later delete it. In this case, there is no entity to save.
So, in your case, you have to make the entity either 'Modified' or 'Unchanged' before you call entityAspect.setDeleted. You can do this by either calling entityAspect.setUnchanged or entityAspect.setModified before calling entityAspect.setDeleted or you can call entityAspect.acceptChanges.
Note that you will also have to insure that the 'clone' entity passes validation and if you have a concurrency field on the entity, you will need to set this appropriately as well.
UPDATE Dec 7th
You can create the book entity in the marked-for-delete state in a single step as shown:
var book = manager.createEntity(entityNames.book,
{ BookID: 1 }, // use initializer to set the key
breeze.EntityState.Deleted); // creates the entity in the Deleted state
Be sure to initialize it with all other properties that are necessary for the entity to pass validation and optimistic concurrency checks on the server.
No problem if you don't have these checks. Not sure how you'd get those values without querying the server if you did have such checks.
got it. cant delete entity while still in added state. I first setModified. then setdeleted. didnt see any side affects.