Reputation: 95
I created a relationship one2many/many2one:
class dev_person(Model):
_name = "dev.person"
_description = "Person"
_columns = {
'name': fields.char('Person', size=128, required=True),
'properties': fields.many2one('dev.property', 'property_id', select=True),
}
class dev_property(Model):
_name = "dev.property"
_description = "Property"
_columns = {
'name': fields.char('Property', size=128, required=True),
'property_id': fields.one2many('dev.person', 'properties'),
}
I want that one dev_person record can be linked with only one dev_property, but that is not the case. I am able to create two person records with the same name and different property, and I don't wont that...
Upvotes: 0
Views: 1452
Reputation: 25072
Your post is confusing. Looking at your models, one dev_person
record can indeed be linked with only one dev_property
. At the same time it is true that you are "able to create two person records with the same name and different property" - this have nothing to do with the many2one relationship. If you create two people with the same name they are still two completely different dev_person
objects. They are not the same person. Of course you can link them with different dev_property
s.
If you want to block this, declare the name
field unique. That way you won't be able to create two people with the same name (make sure this is indeed what you want, since in real life there are different people with the same name).
class dev_person(Model):
_name = "dev.person"
_description = "Person"
_columns = {
'name': fields.char('Person', size=128, required=True),
'properties': fields.many2one('dev.property', 'property_id', select=True),
}
_sql_constraints = [
(
'dev_person_name_uniq',
'unique(name)',
"You can't have two people with the same name!",
),
]
Upvotes: 1
Reputation: 679
From your question I understood that,you want to person and property. And every person must have one property. No two persons can have same Name.
Confusing part is:-
If your intention is to view the persons linked to this property you can use one2many functional field.
For this please try this code:-
from openerp.osv import fields, osv
class dev_person(osv.osv):
_name = "dev.person"
_description = "Person"
_columns = {
'name': fields.char('Person', size=128, required=True),
'properties': fields.many2one('dev.property', 'property_id', select=True),
}
_sql_constraints = [
('name_uniq','unique(name)', 'You cannot have two people with the same name !')
]
class dev_property(osv.osv):
_name = "dev.property"
_description = "Property"
def _compute_persons(self, cr, uid, ids, name, args, context=None):
''' This function will automatically computes the persons related to particular property.'''
result = {}
person_obj = self.pool.get('dev.person')
for person_data in self.browse(cr, uid, ids, context=context):
person_ids = person_obj.search(cr, uid, [('standard_id', '=', person_data.id)], context=context)
result[person_data.id] = person_ids
return result
_columns = {
'name': fields.char('Property', size=128, required=True),
'person_ids': fields.function(_compute_persons, method=True, relation='dev.person', type="one2many", string='Persons'),
}
Hope this helps.
Upvotes: 1