如果模型的数据库与 Odoo 中的其他模型的数据库有 Many2many 关系,如何更新模型的数据库?
How to update the Model's database if it has Many2many relation with other Model's database in Odoo?
我有 2 个模型 "Book" 和 "Author"。我在 them.If 之间有 Many2many 关系,其中一位作者已从作者数据库中删除,我应该删除所有由此 author.I 写的书已经尝试了很多方法,但我是 Odoo 的新手。所以我不能。解决方案是什么?谢谢
Book.py
# -*- coding: utf-8 -*-
from odoo import models, fields,api
class Book(models.Model):
_name = 'about.book'
_description = 'Book Information'
_order = 'publication_date desc, name'
isbn = fields.Char('ISBN',required=True)
name = fields.Char('Title', required=True)
publication_date = fields.Date('Publication Date')
author_ids = fields.Many2many('about.author', select=True, required=True,string='Authors')
_sql_constraints = [('isbn_uniq', 'unique (isbn)','ISBN already exists!')]
@api.constrains('publication_date')
def _check_publication_date(self):
for r in self:
if (r.publication_date > fields.Date.today()) and (r.publication_date == False):
raise models.ValidationError('Publication date must be in the past !')
@api.constrains('author_ids')
def has_author(self):
for r in self:
if r.author_ids == False:
raise models.ValidationError('Book must have at least 1 author!')
@api.one
def unlink(self):
rule = self.env['about.book']
if rule.search([('author_ids', '=', False)]):
rule.unlink()
Author.py
from odoo import models, fields,api
class Author(models.Model):
_name='about.author'
_inherits = {'res.partner' : 'partner_id'}
partner_id = fields.Many2one('res.partner', string="Author")
is_book_author= fields.Boolean('Is Book Author',required=True,default=False)
有一件事我不明白如果这本书是两个作者写的怎么办!
如果不是这种情况,那么关系应该是 one2many。
你说这两个模型之间有many2many关系:
- 您在书籍模型中声明了 many2many 字段
author_ids
。
# override unlink of Author not book
class Author(models.Model):
_name='about.author'
......
......
@api.multi
def unlink(self):
"""when delete author we should delete his books"""
books = self.env['about.book'].search([('author_ids', 'in', self.ids)]
if books:
books.unlink()
return super(Author, self).unlink()
- 学习的第二种情况,如果在
author
模型中声明了 many2many 字段,假设:book_ids
# override unlink of Author not book
class Author(models.Model):
_name='about.author'
@api.multi
def unlink(self):
"""when delete author we should delete his books"""
# use mapped to return all list of books of all record that
# will be removed to call unlink one time avoid loop
books = self.mapped('books_ids')
if books:
books.unlink()
return super(AuthorClass, self).unlink()
我有 2 个模型 "Book" 和 "Author"。我在 them.If 之间有 Many2many 关系,其中一位作者已从作者数据库中删除,我应该删除所有由此 author.I 写的书已经尝试了很多方法,但我是 Odoo 的新手。所以我不能。解决方案是什么?谢谢
Book.py
# -*- coding: utf-8 -*-
from odoo import models, fields,api
class Book(models.Model):
_name = 'about.book'
_description = 'Book Information'
_order = 'publication_date desc, name'
isbn = fields.Char('ISBN',required=True)
name = fields.Char('Title', required=True)
publication_date = fields.Date('Publication Date')
author_ids = fields.Many2many('about.author', select=True, required=True,string='Authors')
_sql_constraints = [('isbn_uniq', 'unique (isbn)','ISBN already exists!')]
@api.constrains('publication_date')
def _check_publication_date(self):
for r in self:
if (r.publication_date > fields.Date.today()) and (r.publication_date == False):
raise models.ValidationError('Publication date must be in the past !')
@api.constrains('author_ids')
def has_author(self):
for r in self:
if r.author_ids == False:
raise models.ValidationError('Book must have at least 1 author!')
@api.one
def unlink(self):
rule = self.env['about.book']
if rule.search([('author_ids', '=', False)]):
rule.unlink()
Author.py
from odoo import models, fields,api
class Author(models.Model):
_name='about.author'
_inherits = {'res.partner' : 'partner_id'}
partner_id = fields.Many2one('res.partner', string="Author")
is_book_author= fields.Boolean('Is Book Author',required=True,default=False)
有一件事我不明白如果这本书是两个作者写的怎么办! 如果不是这种情况,那么关系应该是 one2many。
你说这两个模型之间有many2many关系:
- 您在书籍模型中声明了 many2many 字段
author_ids
。
# override unlink of Author not book
class Author(models.Model):
_name='about.author'
......
......
@api.multi
def unlink(self):
"""when delete author we should delete his books"""
books = self.env['about.book'].search([('author_ids', 'in', self.ids)]
if books:
books.unlink()
return super(Author, self).unlink()
- 学习的第二种情况,如果在
author
模型中声明了 many2many 字段,假设:book_ids
# override unlink of Author not book
class Author(models.Model):
_name='about.author'
@api.multi
def unlink(self):
"""when delete author we should delete his books"""
# use mapped to return all list of books of all record that
# will be removed to call unlink one time avoid loop
books = self.mapped('books_ids')
if books:
books.unlink()
return super(AuthorClass, self).unlink()