preload = fields.Boolean(related='project_id.preload', string='Preload Templates')
part_template_ids = fields.Many2many(
'project.part.template', string='Part Templates', required=True,
default='_default_part_template_ids')
def _default_part_template_ids(self):
domain = [('case_default', '=', True)]
return self.env['project.part.template'].search(domain)
my goal is to change part_template_ids default based on preload field. If preload is True then part_template_ids default='_default_part_template_ids' if preload is false then default for part_template_ids is false too. how can i do this?
At first you have to add a default value to preload
preload = fields.Boolean(
related='project_id.preload', string='Preload Templates',
default=False)
That will trigger onchange events, even on initial creation. You can use that to fill default values for other fields.
#api.onchange('preload')
#api.multi
def onchange_preload(self):
""" Preloads part templates if set to true"""
if self.preload:
domain = [('case_default', '=', True)]
self.part_template_ids = self.env['project.part.template'].search(domain)
else:
self.part_template_ids = self.env['project.part.template']
Related
I would like to filter or return certain data sets with the same "lookup_ref" but I would like to be able to look up multiple "lookup_ref" at one time.
I have searched and I can't find a way of doing this... my thoughts are:
with form checkboxes to see which data sets the user would like to return
use that input to create a list
only return data base entries which have the "lookup_ref" in that list.
have I gone down the wrong route? is there a better way of doing this?
models.pys
class qtable(models.Model):
lookup_ref_choices = (
('gen', 'gen'),
('stolen_car', 'stolen_car'),
('assault', 'assault'),
)
lookup_ref = models.CharField(choices = lookup_ref_choices, max_length=20)
quest = models.CharField(max_length = 500)
order = models.FloatField(default = None)
views.py
def get_user_input(request):
form = Input_Form
context = {'form':form}
return render(request, 'input.html',context )
forms.py
class Input_Form(forms.ModelForm):
class Meta:
model = qtable
fields = "__all__"
exclude = ['quest', 'order']
qtable = forms.ModelMultipleChoiceField(
widget = forms.CheckboxSelectMultiple,
queryset = qtable.objects.all()
)
i'm new in odoo dev and i'm trying to make the field value 'recommanded_price' (from marketplace.marketplaceinfo model) change when 'formula' field changes (which is in the marketplace.marketplace model)
I used the #api.depends decorator but it doesn't trigger the function
Here's the essential of my code
class marketplace(models.Model):
_name = 'marketplace.marketplace'
_description = 'Marketplace'
formula = fields.Char('Formula')
product_ids = fields.One2many('marketplace.marketplaceinfo', 'mkp', 'Products')
class marketplace_info(models.Model):
_name = 'marketplace.marketplaceinfo'
_description = 'Information about a product marketplace'
mkp = fields.Many2one('marketplace.marketplace','Marketplace',ondelete = 'cascade',required = True,
help='Marketplace of this product'
)
product_tmpl_id = fields.Many2one(
'product.template', 'Product Template',
index=True, ondelete='cascade')
price = fields.Float('Price', default=0.0, digits=dp.get_precision('Product Price'),
help="The selling price for a product within the marketplace")
recommonded_price = fields.Float('Recommended Price', compute='_compute_recommonded_price' ,default=0.0, digits=dp.get_precision('Product Price'),
readonly = True, help="The recommended selling price for a product within the marketplace")
#api.depends('mkp.formula')
def _compute_recommonded_price(self):
#do something
#api.depends
This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is altered by ORM or changed in the form:
#api.depends('name', 'an_other_field')
def afun(self):
pass
Other then you can use another decorator
#api.onchange
This decorator will trigger the call to the decorated function if any of the fields specified in the decorator is changed in the form:
#api.onchange('fieldx')
def do_stuff(self):
if self.fieldx == x:
self.fieldy = 'toto'
Difference :
In previous sample self corresponds to the record currently edited on the form. When in on_change context all work is done in the cache. So you can alter RecordSet inside your function without being worried about altering database. That’s the main difference with #api.depends
REF : enter link description here
I have field declared in the following way:
field = serializers.HyperlinkedRelatedField(
view_name='field-detail',
source='feature',
queryset=Field.objects.all()
)
Do you know how I can temporarily set such field as read_only ?
Unfortunately such construction doesn't work :(
serializer.Meta.extra_kwargs = {
'field': {'queryset': None, 'read_only': True}
}
it works fine, when field is declared as a ForeignKey in the Model e.g.
class Foo(models.Model):
field = models.ForeignKey(...)
class FooSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Foo
fields = ('url', 'field',...)
and then (as I mention above), or even:
serializer.Meta.read_only_fields = ('field',)
Try to pass read_only property while declaring the field when you are using HyperlinkedRelatedField
field = serializers.HyperlinkedRelatedField(
view_name='field-detail',
lookup_field='feature',
# set read_only to True
read_only=True
)
Read docs: hyperlinkedmodelserializer
In my Laravel 5 project, I would like to increase the number of views when a visitor come/refresh on the product page like:
$rds = Product::whereId($id)->first();
$rds->hits++;
$rds->save();
The problem is that the field: updated_at automatically update, which is not what I want at all because I would like to change only field: hits, not update_at field.
If I set $rds->updated_at = false; the field is updated to be '0000-00-00 00:00:0'.
Could you advise how to prevent updated_at field from automatically changing for only certain function?
Best Regards,
Naren
Set the timestamps to false to disable updating created_at and updated_at.
$rds = Product::whereId($id)->first();
$rds->hits++;
$rds->timestamps = false;
$rds->save();
You can have a look at the code by Laravel here in performUpdate method:
\Illuminate\Database\Eloquent\Model.php
// First we need to create a fresh query instance and touch the creation and
// update timestamp on the model which are maintained by us for developer
// convenience. Then we will just continue saving the model instances.
if ($this->timestamps && Arr::get($options, 'timestamps', true)) {
$this->updateTimestamps();
}
it's not $rds->updated_at = false, should be $rds->timestamps = false
Simply put the below in your model it would disable timestamp
public $timestamps = false;
or append to the body of the method, below
$rds->timestamps = false;
NB: The first would disable timestamps permanently, while the other would not allow update of timestamp on your edit.
---models.py---
class Products(models.Model):
category = models.ForeignKey(Category)
name= models.CharField(max_length=120, unique=True)
slug = models.SlugField(unique = True)
price = models.IntegerField(default=100)
class Image(models.Model):
property = models.ForeignKey(Products, related_name='images')
image = models.ImageField(upload_to='static/images/home',blank=True,null=True)
---views.py----
if request.is_ajax():
query = Products.objects.all()
p = Paginator(query, 4)
pagedata = p.page(1)
jsondata = serializers.serialize('json', pagedata.object_list)
data = json.dumps({'id' : jsondata,})
return HttpResponse(data, content_type='application/json')
now in ajax data are in (pk, category, name, slug ,price)
but i also want foreign key field i.e 'image' using reverse lookup in ajax. i have already tried list but i want to do using reverse lookup..
You cannot use serializers.serialize and then json.dumps on your stuff. serializers.serialize is basically doing the same thing as json.dumps, to convert a python object into json string. What you need to do is manually construct a dictionary that contains all your data. You might need to loop through the Product list because there's no way to query for reverse relationships for each item in one run.