Custom field called instead of the default field 'name' in m2o field in odoo - odoo-10

There is a field name partner_id in model mail.channel.partner. I want that the partner_id return "Joestar" instead of it's full name 'Joseph joestar'. This should be apply only on this model .
How can I modify the partner_id(m2o) so that it will return only the last string. The goal of this is to hide the full name of the users/partners that's why I need the last string only.
The goal is when we interact to our customer through our site, we don't want to reveal our full name instead, we want to display only the last name.
In the image below, The encircled data is the name I want to modify to last name instead of full name.
enter image description here

Hello Marychan,
Please Override name_get method that'll already define in res.partner model like this:
#api.multi
def name_get(self):
result = []
for rec in self:
result.append((rec.id, "%s" % (rec.name.strip().split()[-1])))
return result
That will give you only last word of name.
If you want to check the url for you model:-
from openerp import http
print http.request.env['ir.config_parameter'].get_param('web.base.url') # BASE URL
print http.request.httprequest
print http.request.httprequest.full_path
Check before stripping and spliting

Related

Django REST Framework - access verbose_name of fields in ModelSerializer

Say I have the following Model:
class Book(Model):
title = CharField(verbose_name="Book title")
and a ModelSerializer:
class BookSerializer(ModelSerializer):
class Meta:
model = Book
fields = "__all__"
I would like to have a function get_verbose_names which returns verbose names of the fields in the model. This is what I have so far:
def get_verbose_names(serializer):
return [field.label for field in serializer.get_fields().values()]
It seems to work fine but problems occur when I use this for the builtin User model. The only fields which work are ID, E-mail, Active, Superuser status and Staff status. The special thing about those fields is that their verbose name differs from their name. Django REST Framework is probably hiding a super-smart logic which checks this and refuses to set the field label to its verbose name in such cases.
Do Django REST Framework's fields have the verbose names hidden somewhere, or they don't copy them from the original Django model fields at all and I am screwed? Or will the trick be to override this logic? I tried and could not find it.
Django REST Framework really has the mentioned "super-smart logic". It is the function needs_label in utils.field_mapping:
def needs_label(model_field, field_name):
"""
Returns `True` if the label based on the model's verbose name
is not equal to the default label it would have based on it's field name.
"""
default_label = field_name.replace('_', ' ').capitalize()
return capfirst(model_field.verbose_name) != default_label
Probably the easiest way to bypass this annoying feature is to do this:
def get_verbose_names(serializer):
return [field.label or name.replace("_", " ").capitalize()
for name, field in serializer.get_fields().items()]
Explained in words, check the field label and if none was auto-generated for it, use the needs_label logic to determine it.

Odoo 10 - Duplicate supplier info for a given product_template

I have:
a) given product_template_id (i.e. id 100) and
b) a duplicated product_template_id (i.e. id 200) created using copy() method
copy() method copies only product.template model, so suppliers for that specific product are not copied.
I would like to duplicate all suppliers for that model, but now I am wondering which is the right way to do it in Odoo.
If I understood the model properly suppliers prices for a given product are stored in product_supplierinfo table, where each record that points to a given product_tmpl_id specifices a supplier price/qty for a given product_template.
Which would be the way in Odoo to search for all records that point to a given product_tmpl_id (i.e. 100), duplicate them changing product_tmpl_id to the new one (i.e. 200)?
Excerpt from the ORM Documentation:
copy (bool) -- whether the field value should be copied when the record is duplicated (default: True for normal fields, False for One2many and computed fields, including property fields and related fields)
The field you're referring to is seller_ids, whose field definition is below:
seller_ids = fields.One2many('product.supplierinfo', 'product_tmpl_id', 'Vendors')
The copy attribute is not explicitly defined, so it is False by default (as explained in the documentation above). If you want this field to copy along with the other values during the standard product "Duplicate" (copy method), you can do this:
class ProductTemplate(models.Model):
_inherit = 'product.template'
# This only changes the copy attribute of the existing seller_ids field.
# All other attributes (string, comodel_name, etc.) remain as they are defined in core.
seller_ids = fields.One2many(copy=True)
Alternatively
If you want to only have the field copied sometimes, you can extend the copy method to look for a specific context value and only copy based on that.
# This may take some tweaking, but here's the general idea
#api.multi
def copy(self, vals):
new_product = super(YourClass, self).copy(vals)
if vals.get('copy_sellers'):
new_product.seller_ids = self.seller_ids.copy({'product_id': new_product.id})
return new_product
# Whatever you have calling the copy method will need to include copy_sellers in vals
vals.update({'copy_sellers': True})
product.copy(vals)

$this->getRequest()->getParam() not working in category controller

I am working on retriving the manufacturer attribute from url
localhost/magento/index.php/test-pro.html?manufacturer/4
So i used $this->getRequest()->getParam('manufacturer')
I did not get any output.
But when i changed the url as localhost/magento/index.php/test-pro.html?manufacturer=4
(/ replaced by =), i get proper output.
But i need the url should be localhost/magento/index.php/test-pro.html?manufacturer/4
and want to fetch the product related to that manufacturer id 4.
Somebody help me.
In your query string ?manufacturer=4 will give you the value for manufacturer i.e. 4, while manufacturer/4 will give you no value as its not being treated as query string.
Also the param will be and the param will be manufacturer/4 and not manufacturer.
To achieve what you require, you can do sometinhg like below.
$currentUrl = 'localhost/magento/index.php/test-pro.html?manufacturer/4';
$parts = parse_url($currentUrl);
$val = explode('/',$parts['query']);
Mage::register('manufacturer',$val[1]);
$menuVal = Mage::registry('manufacturer');
echo $menuVal; //prints 4
This is a sample code by which you can get the query string value even if you use / instead of =.

How to retrieve the field name of a ShapeFile feature field?

I am using gdal-ruby to parse ESRI ShapeFiles like in this demo. I want to iterate through all features in order to push the field values into a database. However, I cannot find out how to retrieve the name of each field which I need to match the database column. By now I can only work with the field index of the field such as:
dataset = Gdal::Ogr.open(filename)
number_of_layers = dataset.get_layer_count
number_of_layers.times do |layer_index|
layer = dataset.get_layer(layer_index)
layer.get_feature_count.times do |feature_index|
feature = layer.get_feature(feature_index)
feature.get_field_count.times do |field_index|
field_value = feature.get_field(field_index)
# How can I find out the name of the field?
puts "Value = #{field_value} for unknown field name"
end
end
end
I checked the available methods with irb and looked into the API documentation. It seems as if I am searching for the wrong terms.
Looking at the OGR API itself, I think you need to go via feature.GetDefnRef, to get the feature definition, then .GetFieldDefn for the relevant field, and finally .GetNameRef...?
...
feature.get_field_count.times do |field_index|
defn_ref = feature.get_defn_ref
field_defn = defn_ref.get_field_defn(field_index)
field_name = field_defn.get_name
field_value = feature.get_field(field_index)
puts "Value = #{field_value} for field named #{field_name}"
end
...
ds = ogr.Open(filename, 1)
layer = ds.GetLayer()
for i in range(len(layer.schema)):
print(layer.schema[i].name)

SPQuery sorting issue

I have this SPListItem.Folder in sharepoint that contains a property named "Asset ID".
I have this data in my list
Asset ID | Name | Asset Type
1 | GamesFolder | Games
2 | AppsFolder | softwares
3 | MusicFolder | music
In my code I did this
SPList objList = web.Lists["MyList"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='Asset ID' Ascending='FALSE'/></OrderBy>";
query.ViewAttributes = "Scope=\"Recursive\"";
query.RowLimit = 1;
SPListItemCollection items = objList.GetItems(query);
return objList.Items[0].Folder.Properties["Asset ID"].ToString();
I use .Folder because every entry in the list is a DocumentSet.
The returned value is always "1". I don't know what's wrong why my sorting
doesn't work at all.
Please help me resolve this issue. Thanks.
Hi Carls I think there is issue for field name. U include space in field name
If you want to avoid having to seek out what the internal name of a particular field is, when you first name your column, do not include any spaces or special characters. Once the field (column) has been created, go back and rename the field to include the spaces or special characters as desired. SharePoint will still retain the original field name without spaces and you can use that directly in your query without issue.
or use its internalName:
query.Query = "<OrderBy><FieldRef Name='Asset_x0020_ID' Ascending='FALSE'/></OrderBy>";
A little late but if you are having issues, you may be able to use all or a part of the following gist:
https://gist.github.com/trgraglia/4672176
And as the accepted answer states, the field name is the issue. You need to use the static name of the field. The static name will always stay the same. Even if the column is renamed. So you should get the column from the column collection by display name and then get the static name from the properties.

Resources