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()
Related
I'm trying to give a field in Django a default value = value of pk, is this possible ?
I've found an old question about giving a field default = value of another field but it doesn't really help when working with pk value :
How do I set default field value to value of other field in a Django model?
Basicly is something like this possible using pk instead of "name" variable ? :
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100,default=name)
def clean(self):
self.fullname=name
Thanks.
Django creates auto primary keys, the model will have a field called "id" and it is also called "pk"
If you access your database you'll be able to see it.
If you want to set your own primary key you will need to do something like:
custompk = models.someField(primary_key=True)
As your question is vague I've attempted to answer to the best of my ability.
If you are trying to access a PK/ID of an object in a view let me know and I can help with that as well. :)
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.
I have a One2many field that depends on the value of a Many2one field in the form. The related model for the One2many field depends upon the value of the Many2one field.
i_name = fields.Many2one('mod.items',string="Item")
i_fields = fields.One2many('mod.x_fields','field_name',string="Item Characteristics")
Here, the 'mod.x_fields' (model name) should vary according to the i_name field value. Is it possible to make such a field? Can this be done using api.onchange?
Yes you can put #api.onchange in mod.x_fields model and update your field according to your parent model access like self.field_name.i_name
This is how i had to do it. I had to use reference field for i_name and give the model names as selection options. As given here.Though this answer is different from what i wanted(the question), this is the nearest i got.
i_name = fields.Reference(selection='_referencable_models',string='Item')
#api.model
def _referencable_models(self):
return [('mod.item1_fields', 'Item 1'),('mod.item2_fields','Item 2'),...]
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
Ok, I am working on a Django application with several different models, namely Accounts, Contacts, etc, each with a different set of fields. I need to be able to allow each of my users to define their own fields in addition to the existing fields. I have seen several different ways to implement this, from having a large number of CustomFields and just mapping a custom name to each field used by each user. I have also seem recommendations for implementing complex mapping or XML/JSON style storage/retrieval of user defined fields.
So my question is this, has anyone implemented user defined fields in a Django application? If so, how did you do it and what was your experience with the overall implementation (stability, performance, etc)?
Update: My goal is to allow each of my users to create n number of each record type (accounts, contacts, etc) and associate user defined data with each record. So for example, one of my users might want to associate an SSN with each of his contacts, so I would need to store that additional field for each Contact record he creates.
Thanks!
Mark
What if you were to use a ForeignKey?
This code (untested and for demo) is assuming there is a system-wide set of custom fields. To make it user-specific, you'd add a "user = models.ForiegnKey(User)" onto the class CustomField.
class Account(models.Model):
name = models.CharField(max_length=75)
# ...
def get_custom_fields(self):
return CustomField.objects.filter(content_type=ContentType.objects.get_for_model(Account))
custom_fields = property(get_fields)
class CustomField(models.Model):
"""
A field abstract -- it describe what the field is. There are one of these
for each custom field the user configures.
"""
name = models.CharField(max_length=75)
content_type = models.ForeignKey(ContentType)
class CustomFieldValueManager(models.Manager):
get_value_for_model_instance(self, model):
content_type = ContentType.objects.get_for_model(model)
return self.filter(model__content_type=content_type, model__object_id=model.pk)
class CustomFieldValue(models.Model):
"""
A field instance -- contains the actual data. There are many of these, for
each value that corresponds to a CustomField for a given model.
"""
field = models.ForeignKey(CustomField, related_name='instance')
value = models.CharField(max_length=255)
model = models.GenericForeignKey()
objects = CustomFieldValueManager()
# If you wanted to enumerate the custom fields and their values, it would look
# look like so:
account = Account.objects.get(pk=1)
for field in account.custom_fields:
print field.name, field.instance.objects.get_value_for_model_instance(account)