i have a model that inherits res.partner model
class supplierDetails(models.Model):
_inherit = 'res.partner'
farmer_code = fields.Char(string="Farmer's Code")
Now i want the values from farmer_code in another model, so as per this answer, i reached this far.
class productTest(models.Model):
_name = 'quality.physical'
_inherit = 'res.partner'
frm_code_ids = fields.Many2one('res.partner',string="Farmer Code")
frm_cod = fields.Char(related='frm_code_ids.farmer_code',store=True,readonly=True)
Now i am getting KeyError: 'farmer_code'. What do i do to fix this? Thanks in advance.
Your code should work, repeat the process step by step make sure the res.partner have this field you may forget to put the class in the __init__.py file.
# related field without store is like a compute field it's computed on fly
related_field_name = fields.FieldType(related='your_m2o.target_field_name')
If you put store=True it will be added to the database and recomputed whenever you change the value of many2one.
You can go more than one level ex: your_m2m.another_m2o_field.target_field_name.
Field type should be the same as the target field.
Just make sure the target model have that field.
Related
In odoo v13, the crm.lead model is inherited and modified by the sale_crm module.
In the sale_crm module, the model crm.lead is inherited and a one2many field is added, order_ids. This is an array of sales orders associated with the lead.
I am trying to inherit the crm.lead model, and create a new field that is computed using the order_ids field.
I added sale_crm in the manifest dependencies
I inherit the crm.lead model and attempt to concat the names of all the associated SOs:
class Co7Lead(models.Model):
_inherit = "crm.lead"
so_list = fields.Text(
compute='_get_sos_text',
string="Text list of associated SOs",
help="A comma separated list of SOs associated with this lead")
def _get_sos_text(self):
txt = ""
for order in super(Co7Lead, self).order_ids:
txt += order.name + ""
return txt
Unfortunately, this causes a stack overflow (haha!)
I believe I need to use .browse on the order_ids field but I'm not able to find any examples on how to do this.
The compute method must assign the computed value to the field. If it uses the values of other fields (order_ids.name), it should specify those fields using depends().
You don't need to use super here, self is a record set, so loop over it to access the value of order_ids for each record.
Example:
#api.depends('order_ids.name')
def _get_sos_text(self):
for lead in self:
lead.so_list = "\n".join(order.name for order in lead.order_ids)
I'm trying to make a manytomany field from a model that is not the model that the manytomany field will contain a list of. e.g.
class Following(models.Model):
following_id = models.AutoField(primary_key=True)
following_user = models.ForeignKey(User, models.DO_NOTHING, related_name="following_user")
following = models.ManyToManyField(User, related_name="following")
This looks all good to me, but when I try to enter the shell and do something like User.following.add(OtherUser), I get an error saying that it was expecting OtherUser to be an instance of Following. Why is this? Did I not specify that the ManyToManyField was storing User instances when I declared the following variable?
models.ManyToManyField(**User**, related_name="following")
1 - Create a user : user = User(); user.save()
2 - Create a following : following = Following(); following.save()
3 - Add the user to the following : following.following.add(user)
You can reference a model with other model only once. But you are using User model two times, one with following_user field and other with following field. Look below your model.
class Following(models.Model):
following_id = models.AutoField(primary_key=True)
following_user = models.ForeignKey(*User*, models.DO_NOTHING, related_name="following_user")
following = models.ManyToManyField(*User*, related_name="following")
try this:
Another side effect of using commit=False is seen when your model has a many-to-many relation with another model. If your model has a many-to-many relation and you specify commit=False when you save a form, Django cannot immediately save the form data for the many-to-many relation. This is because it isn’t possible to save many-to-many data for an instance until the instance exists in the database.
To work around this problem, every time you save a form using commit=False, Django adds a save_m2m() method to your ModelForm subclass. After you’ve manually saved the instance produced by the form, you can invoke save_m2m() to save the many-to-many form data. For example:
Here is the declaration of my field:
partner_id = fields.Many2one(string="Child", comodel_name="res.partner")
I would like to know if it is possible to target another field of the table res.partner besides the "name"?
And why not a field that depends on another field?
It is possible to declared options?
Thanks for your help !
EDIT :
I'm trying to understand one thing.
When I export my partner_id field via the Odoo interface, I do not have a value of type res_partner_5096 but the value of another field which is an external identifier. "N0000542145"
This is what I need to be able to prepare an import file
On the other hand I have a second field "foyer_id" which exports me something of the style "relation_foyer_6055". I understand that it is the identifier that corresponds to registration of this person.
Yet my fields are declared the same way.
partner_id = fields.Many2one(string="Child", comodel_name="res.partner")
foyer_id = fields.Many2one(string="Foyer", comodel_name="horanet.relation.foyer")
I can not understand why this difference when exporting these two fields for the same partner.
An idea ?
I think you exactly looking for the "_rec_name" attribute. Odoo models uses a field as record name to display records in context where a representative “naming” is necessary. If you don't set _rec_name, model uses name field as record name by default.
Class HoranetRelationFoyer(models.Model):
# ...
_rec_name = 'my_field'
my_field = fields.Char()
In OpenERP v7, I need to get all the fields of res.partner model in the travel.partner model
class travel_partner(osv.osv):
_name = "travel.partner"
_inherit = "res.partner"
When creating records in "travel.partner" model works fine. but when trying to create a partner record or user or company gives following error
AttributeError: 'Field image not found in browse_record(travel.partner, 68)'
How can i solve this? or is there any alternate solution other than re coding all the fields to the new table?
You can try by defining relation between travel.partner and res.partner same as product.template and product.product by adding _inherits not _inherit, it will allow you to access all the parent fields in child model.
At current situation I can not say anything without looking your entire code because logically nothing is wrong in your question, so it's better if you should add all the details or just implement another one.
Because when you create res.partner is must not to go for travel.partner as it's child of res.partner.
I got the answer.
The error was because of the functional field with store condition. In res.partner, there were fields image_medium and image_small using store conditions with key as res.partner. In my new model,I re defined the code with store condition key changed to travel.partner which fixed the issue
I've defined a models.py with a "FirstClass" which contains a ForeignKey relathionship to "SecondClass". The relathionship can't be Null.
The SecondClass is very expansive (90.000 records), and when i display the FirstClass html form, it requires too many time generating the "select box" field.
Therefore, when I let user update the object (I use create_update.update_object generic view), i don't want to display and update the value of the foreignkey field, but i don't know how to do this...
Create a ModelForm and pass it into the view, according to the docs.
Since the foreign key should always exist upon creation, it's safe to ignore it in the update.
class MyModelForm(forms.ModelForm):
class Meta:
model = FirstClass
exclude = ('SecondClass',)
# urls.py
(r'^foo/(?P<object_id>\d+)/$','django.views.generic.create_update.update_object',
{'form_class': MyModelForm})