We can read and search data from Odoo using XML-RPC like it's explained on:
https://www.odoo.com/documentation/8.0/api_integration.html
But, for "search" all the examples are using juste simple fields, there's no example to filter on relationship fields.
There is my case:
I have a partner(res.partner) who has a related user(res.user) on "user_id" field.
In this example, I try to get the ids of partners related to the user(id = 36) on "user_id" field, but it doesn't work:
ids = models.execute_kw(db, uid, password,
'res.partner', 'search',
[[['user_id', '=', 36]]],
{'limit': 10})
Any ideas ?
Thank you,
Related
How can I restrict the write permissions for a field to a specific group ?
I want to check if a user is in a specific group with id 46. If the user is in this group, he should be allowed to write in this field. If he is not in this group, he should not be allowed to write.
The field is a custom field, editing the domain with the studio app I think I should avoid.
My field:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)]}"/>
I tried the following, but it did not work:
I created a new field using the studio app. Field type is boolean.
In the advanced properties I wanted to define the compute for the field. In dependencies I gave "user_id" and in the compute field I gave
for record in self:
user_id.has_group('__export__.res_groups_46_eff9dc52')
The boolean field should be set to true if the user is in a certain group.
Not sure if I can give you the best answer there is.
But for me, I'd personally create a Boolean field in the view's associated model, with its default field a lambda function checking if the user belongs to the groups you mentioned.
Assuming groups_id is the name of the user groups in model res.users, we have:
class ResUsers(models.Model):
_inherit = "res.users"
can_write_codename = fields.Boolean(default=lambda self: self.groups_id in ("model_name.group_name"))
Then in your xml file, you can include can_write_codename inside attrs, like this:
<field name="customer_codename" placeholder="Codename" attrs="{'invisible':['|',('customer_rank','=', 0),('is_company','=', False)], 'readonly': [('can_write_codename', '=', 'True')]}"}"/>
I'm trying to change the state of a created invoice from 'draft' to 'posted' using the Web APIs (Python) by referring to this documentation : https://www.odoo.com/documentation/13.0/webservices/odoo.html
I'm updating the invoice as follows :
def makeInvoicePosted(invoice_id):
invoice_ids = []
invoice_ids.append(invoice_id)
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
print(common)
uid = common.authenticate(db, username, password, {})
print("makeInvoicePosted : Odoo Admin User Id : ", uid)
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
models.execute_kw(db, uid, password, 'account.move', 'write', [[invoice_id], {'state':"posted"}])
But I'm getting this error : odoo.exceptions.ValidationError: ('Posted journal entry must have an unique sequence number per company.', None)\n'
What could be causing this ? is there something missing in the request?
Thanks in advance!
I recommend to use Odoo's workflow and business logic here by calling post instead of directly writing the state.
models.execute_kw(db, uid, password, 'account.move', 'post', [[invoice_id],])
Why: because there are a lot of checks and also a lot of things done in this method, you could miss or just do wrong (invoices are very complex). You probably will find some mistakes in your calls right before doing the post, because of the checks in it.
I am trying to update some information on my odoo product with PHP and xmlrpc.
This is my code for update product name.
$models->execute_kw($db, $uid, $password, 'product.product', 'write',
array(array(5), array('name'=>"Newer product 3",'type'=>"consu")));
Now I want to change a "Quantity On Hand" field so I trying this code :
$models->execute_kw($db, $uid, $password, 'product.product', 'write',
array(array(5), array('name'=>"Newer product 3",'type'=>"consu",'qty_available'=>'7')));
but it doesn't work, anyone got any ideas how to fix it?
Thank you.
The field qty_available is readonly.
On odoo10 you can use the following operation, python:
product = models.execute_kw(db, uid, password, 'product.product', 'search', [[('default_code', '=', sku)]])
change_id = models.execute_kw(db, uid, password, 'stock.change.product.qty', 'create', [{
'product_id': product[0],
'location_id': 15,
'new_quantity': 20,
}])
models.execute_kw(db, uid, password, 'stock.change.product.qty', 'change_product_qty', [change_id])
Since the field qty_available is read only there is no way to update it.
To update it you'll need to create a stock move
First make sure that the field qty_available really exists in the model. You can check the fields in the model from (via browser)
settings --> database structure --> models
If not make sure sale, stock, product modules are properly installed. By the way in my odoo repository (i.e odoo 9 stable) I even can't see qty_available field in the model product.product . But I can see it in the openerp v7. Probably this field is either removed or the field is inheriting some other modules e.g sale_stock etc.
Hope this will solve you problem.
This is what i trying to do: I have a User model with attributes :name, :lastame and :email. After a method like the following:
providers = User.find_by_sql [ query ]
I'd like all the users in providers to include a new attribute :location.
Is that possible?
In this case, the easiest way will be to simply add the value you want as a column alias in your SELECT clause.
providers = User.find_by_sql "SELECT *, 'The Moon' AS location FROM users"
puts providers.first.location
# => "The Moon"
I have these models:
class Person(models.Model):
user = models.OneToOneField(User)
class Post(models.Model):
author = models.ForeignKey(Person, null=False)
text = models.TextField(null=False, blank=False)
and want a Queryset at least with the below fields,
author.user.username
text
I have read select_related() queries but when I try to use that with this view can't get username field
posts = Post.objects.select_related('person__user')[:10]
can I use Django query or have to use SQL raw ?
Thanks for any help
You can serialize like this:
import json
from django.core.serializers.json import DjangoJSONEncoder
json_data = json.dumps(list(Post.objects.values('author__user__username', 'text')[:10]), cls=DjangoJSONEncoder)
select_related should be called with the field names, not the type.
posts = Post.objects.select_related('author__user')[:10]
for post in posts:
print(post.person.user.username)
print(post.text)
All the select_related does is ensure that the foreign fields can be accessed without extra queries (select_related constructs joins to the relevant tables).