How to set default values to fields in Odoo 14 - javascript

I added a filed company(It's a char field), when creating a payment ,I want to set default value.
When creating new payment, the value wasn't displayed in the form view . But , when printing 'my_company' , I got the correct result.
What's wrong please?
class AccountPayment(models.Model):
_inherit = "account.payment"
#api.model
def get_company(self):
if self.move_type == 'in_invoice':
my_company = self.env.user.company_id.name
self.company = my_company
else:
self.company = ''
company = fields.Char(string='Company ', default=get_company)
Thanks.

You need to return the value.
Here is the correct code
class AccountPayment(models.Model):
_inherit = "account.payment"
def get_company(self):
if self.move_type == 'in_invoice':
my_company = self.env.user.company_id.name
return my_company
else:
return None
company = fields.Char(string='Company Name', default=get_company)

Related

Get Current User when creating record . Odoo 14

I have a field many2one relation to res.users in point of sale like this :
user_id = fields.Many2one(
comodel_name='res.users', string='Responsible',
help="Person who uses the cash register. It can be a reliever, a student or an interim employee.",
default=lambda self: self.env.uid,
states={'done': [('readonly', True)], 'invoiced': [('readonly', True)]},
)
<field string="User" name="user_id"/>
I want to set it 'required' and empty field , but when saving record if the value of 'user_id' was empty , then get the current user.
I tried with this code , but required value was blocking:
#api.model
def _default_user_id(self):
if not self.user_id:
self.user_id = self.env.uid
else:
self.user_id = False
user_id = fields.Many2one(
comodel_name='res.users',
required=True,
help="Person who uses the cash register. It can be a reliever, a student or an interim employee.",
default=_default_user_id,
states={'done': [('readonly', True)], 'invoiced': [('readonly', True)]},
)
How can I do it ?
Thanks.
Change your default code
from
default=lambda self: self.env.uid,
to
default=lambda self: self.env.user,
EDIT-1
In this case, we need to override create() method instead of write logic in default function. With following code, it will check user_id field at the time of record create, if record doesn't have user_id, it will set current logged user.
#api.model
def create(self, vals):
if "user_id" not in vals:
vals.update({"user_id": self.env.user})
record = super().create(vals)
return record
EDIT-2
In this case, we need to write down default function.
def _default_user_id(self):
if not self.user_id:
return self.env.user
return False

How to get current record id in wizard. Odoo14

I added a wizard in action of model 'product.template', I want to get 'active_ids' in wizard, I trying with this code:
class Wizard(models.TransientModel):
_name = "product.wizard"
product_template_ids = fields.Many2many('product.template')
#api.model
def default_get(self, fields):
res = super(Wizard, self).default_get(fields)
res['product_template_ids'] = self.env.context.get('active_ids', False)
return res
But , I got this error : raise exception.with_traceback(None) from new_cause
TypeError: 'int' object is not subscriptable
What's wrong ? Any help please?
Thanks.
Try this:
First check what you are getting with this self.env.context.get('active_ids', False), if it gives id inside list i.e [23] then use many2many filling operation i.e
res['product_template_ids'] = [(6, 0, self.env.context.get('active_ids', False))]
and if it's giving ID i.e 23 then use:
blank_list = []
blank_list.append(self.env.context.get('active_ids', False))
res['product_template_ids'] = [(6, 0, blank_list)]

Extracting users from manytomany field from model Django

I want to extract users who follow specific user. I have two models here
class User(AbstractUser):
pass
class Follower(models.Model):
user = models.ForeignKey(User,null=True, on_delete=models.CASCADE, related_name="follow")
following = models.ManyToManyField(User, null=True, blank=True)
views.py
def profile(request, userq):
usera = User.objects.get(id = userq)
posts = Post.objects.filter(userp = userq).all()[::-1]
print(userq)
try:
follow1 = Follower.objects.filter(following = usera).all()
except:
follow1= []
try:
follow2 = Follower.objects.get(user = usera).following.all()
except:
f = Follower.objects.create(user = usera)
follow2= f.following.all()
print(follow1)
print(follow2)
return render(request,"network/profile.html",{
"posts":posts,"usera":usera,"follow1":follow1,"follow2":follow2
})
How to do achieve this in views.py?
If you need users which are followed by specific user:
Follower.objects.get(user = your_user).following.all()
If you need users who follow specific user:
User.objects.filter(follower__user = your_user)
Django docs

Configure Django model form with jQuery and JavaScript

I have three models :
class Instances(models.Model):
name_of_instances = models.CharField(max_length=255)
url_of_instances=models.URLField(max_length=255,default='')
def __str__(self):
return self.name_of_instances
class Projects(models.Model):
name_of_instances = models.ForeignKey(Instances,on_delete=models.CASCADE)
name_of_jproject = models.CharField(max_length=255)
project_id= models.CharField(max_length=25)
def __str__(self):
return self.name_of_project
class All(models.Model):
choices_instance=Instances.objects.all()
b = [str(i.name_of_instances) for i in choices_instance]
a=[str(i.url_of_instances) for i in choices_instance]
choices_instance=tuple(zip(a,b))
name = models.CharField(max_length=255,choices=choices_instance)
project = models.CharField(max_length=255,)
story = models.CharField(max_length=500)
depends_on = models.CharField(max_length=500, default='')
rfc = models.CharField(max_length=255)
def __str__(self):
return self.name
I have a models form:
class Form(ModelForm):
class Meta:
model = All
fields =('name','project','story','depends_on','rfc')
In template, I want to know what user has drop down in 'name' field , and I want to send the name value to back end function where I will filter out all the projects associated with name value, and send the result to 'project' field as choices (drop down).
i got the solution using java script and ajax on the template

Extjs And Grails How can i prevent ancestors

I work a project which server site is Grails, Client Site is Extjs4.1, I use relational database. In this situation i work fine but when i call store then i facing some problem.
This problem based on parents. when I call store like below..... then i must call its parents but i do not want to call its parents..
def stote(){
def prices = []
Price.getAll()?.each{ v ->
def a = v.article,
b = a.brand,
bt = b.brandType,
gp = b.genericProducts,
c = gp.categories,
m = b.manufacturers
def manufacture = [id:m.id, name:m.name]
def category = [id:c.id, name:c.name]
def genericProduct = [id:gp.id, name:gp.name, m01i001001:category]
def brandType = [id:bt.id, name:bt.name]
def brand = [id:b.id, name:b.name, m01i002001:manufacture, m01i003001:genericProduct, m01i004001:brandType]
def article = [id:a.id, name:a.name, mbcode:a.mbcode, pbcode:a.pbcode, details:a.details, m01i005001:brand]
def price = [id: v.id, m01i006001:article, price:v.price, date:v.date]
prices << price
}
return prices
}
Those are belongTo association key
m01i001001, m01i002001, m01i003001,m01i004001,m01i005001,m01i006001
my belongTo like as
belongsTo : [{
model : '${pkgName}.M01I005001',
associatedName : 'M01I005001' ,
associationKey : 'm01i005001' ,
primaryKey : 'id' ,
foreignKey : 'brandId'
}],
i want to call like this store...
def stote(){
def prices = []
Price.getAll()?.each{ v ->
def a = v.article
def article = [id:a.id, name:a.name, mbcode:a.mbcode, pbcode:a.pbcode, details:a.details]
def price = [id: v.id, m01i006001:article, price:v.price, date:v.date]
prices << price
}
return prices
}
but when i call this store then client side loading problem. I can not lazy its parents. its show always eager.
Please let me know if you have any idea to resolve this error.
Thanks in advance
This is not a solution for this. I create a proxy model for this and i remove all relation which are not necessary then i solve my problem that means i keep only one belongsto so keep only one association key.

Categories

Resources