odoo14 on change only gets called on specific change - python

This is the on change function of one of my models in my odoo14 module:
#api.onchange('staende', 'name', 'length', 'height')
def change(self):
print("\033[92m +++ DEBUG#ONCHANGE +++")
print(self)
print("\033[93m", end="")
weirdly enough, the method only gets called when I change the name of the model but not when I change the length or height nor the staende attribute
this is what the model looks like:
class Halle(models.Model):
_name = 'rectanglemaps.halle'
_inherit = 'mail.thread'
_description = 'desc'
active = fields.Boolean('Active', default=True)
name = fields.Char(string="Name", required=True)
length = fields.Float(string="Länge", required=True)
height = fields.Float(string="Tiefe", required=True)
measurehalle = fields.Float(string="Rastermaß")
plan = fields.Image(string="Plan")
messe_id = fields.Many2one('rectanglemaps.messe', string="Messe")
staende = fields.One2many('rectanglemaps.stand', 'halle_id', string="Stände")

It appears, it was only a problem with hot module reloading. Restarting odoo with the flag -u MODULENAME solved the problem.

Related

How do I get values from a one2many field?

I have three models which are connected with each other.
The first one is an inheriting of product.template (product_template.py) and as you see I have a one2many field which is connected to the second model showed in here.
When I want to access variables from product_template.py with dot notation I can see variable suggestion and I guess it works fine.
class ProductTemplate(models.Model):
_inherit = "product.template"
kestet_ids = fields.One2many('kestet.lines', 'product_template_id', string="Kestet", store=True)
class OnetoMany(models.Model):
_name = "kestet.lines"
partner_id = fields.Many2one("res.partner")
kestet = fields.Char(string="Emri i Kestit(n)")
vlera = fields.Integer(string="Vlera e Kestit(n)")
data = fields.Date()
sel1 = fields.Selection(
[('kontrate', 'Kontrate'), ('levrim', 'Levrim Kredie'), ('karabina', 'Karabina'), ('hipoteka', 'Hipoteka'),
('Celsat', 'Celsat'), ('Ne Perfundim', 'Ne Perfundim'),
('other', 'Tjeter')], string="Menyra Likujdimit")
pagesa1 = fields.Selection([('Cash', 'Cash'), ('Bank', 'Bank'), ('Klering', 'Klering')], string="Menyra Pageses")
product_template_id = fields.Many2one("product.template")
property_contract_id = fields.Many2one("property.contract")
Now when I try to access it from this other model property_contract.py and do the same thing as above, it doesn't give me variable suggestions and I can not access variable values.
class PropertyContract(models.Model):
_name = "property.contract"
_order = 'start_date'
name = fields.Char("Contract Number")
kestet2_ids = fields.Many2one('kestet.lines')
I also tried:
installment_id = fields.Many2one("product.templates")
installment_relation = fields.One2many(related="installment_id.kestet_ids")
It failed giving me results. I tried also the above code but didn't work...
What am I doing wrong here?

how to set default company logo in odoo 12?

I 'am trying to set a default logo for the company, but nothing changes
I tried this code
class logocompany(models.Model):
_inherit = 'res.company'
#api.model
def _default_company_image(self):
image_path = modules.get_module_resource('auto_product', 'static/src/img', 'autoparts.png')
return tools.image_resize_image_big(base64.b64encode(open(image_path, 'rb').read()))
logo = fields.Binary(default=_default_company_image )
any help well be apprcited

AttributeError: 'list' object has no attribute 'objects'

I am trying to run the following which is throwing an
AttributeError: 'list' object has no attribute 'objects'
script.py
#Get Dota2 Item Rarities
dotaItemRarityUrl = 'http://api.steampowered.com/IEconDOTA2_570/GetRarities/v1?'
dotaItemRarityPayload = {'key': settings.SOCIAL_AUTH_STEAM_API_KEY,
'language': 'en',
}
dotaItemRarityInfo = requests.get(dotaItemRarityUrl, params=dotaItemRarityPayload)
dotaItemRarity = dotaItemRarityInfo.json()
dotaItemRarity = dotaItemRarity['result']['rarities']
print(dotaItemRarity)
#print(dotaItemQualities)
#Populate Database With Item Rarities that do NOT exist already
for rarity in dotaItemRarity:
print rarity
irarityId = rarity['id']
irarityProperName = rarity['localized_name']
irarityInternalName = rarity['name']
irarityColor = rarity['color']
q = dotaItemRarity.objects.filter(rarityId=irarityId)
print q
if len(q) == 0:
newRarity = dotaItemRarity(rarityId=irarityId,
rarityProperName=irarityProperName,
rarityInternalName=irarityInternalName,
rarityColor=irarityColor)
newRarity.save()
models.py
class dotaItemRarity(models.Model):
rarityId = models.IntegerField(max_length=3,primary_key=True)
rarityProperName = models.CharField(max_length=60)
rarityInternalName = models.CharField(max_length=50)
rarityColor = models.CharField(max_length=30)
def __unicode__(self):
return self.rarityInternalName
I am using south to handle migrations and have tried multiple options to fix this e.g. removing the tables and rebuilt them. As far as I can tell this should work, can anyone point me in the right direction.
dotaItemRarity is a list, and it has no objects attribute:
q = dotaItemRarity.objects.filter(rarityId=irarityId)
That's because you bound it to a list from your JSON result:
dotaItemRarity = dotaItemRarityInfo.json()
dotaItemRarity = dotaItemRarity['result']['rarities']
It is not a Django model, as you appear to expect it to be.
If you had the dotaItemRarity Django model imported into script.py, then the name is no longer bound to that model, as you replaced it with the list.
Rename the list to use a different name that doesn't mask the model.
Note that the Python style guide recommends that you use CamelCase names for classes (including Django models), to avoid such mistakes.
Following PEP 8 to refactor your code a little, as well as using some clearer naming an practices:
models.py:
class DotaItemRarity(models.Model):
rarity_id = models.IntegerField(max_length=3, primary_key=True)
rarity_proper_name = models.CharField(max_length=60)
rarity_internal_name = models.CharField(max_length=50)
rarity_color = models.CharField(max_length=30)
def __unicode__(self):
return self.rarity_internal_name
script.py:
#Get Dota2 Item Rarities
url = 'http://api.steampowered.com/IEconDOTA2_570/GetRarities/v1'
payload = {'key': settings.SOCIAL_AUTH_STEAM_API_KEY, 'language': 'en'}
response = requests.get(url, params=payload)
rarities = response.json()['result']['rarities']
for rarity in rarities:
rarity_id = rarity['id']
try:
DotaItemRarity.get(rarity_id=rarity_id)
except DotaItemRarity.DoesNotExist:
new_rarity = DotaItemRarity(
rarityId=rarity_id,
rarity_proper_name=rarity['localized_name'],
rarity_internal_name=rarity['name'],
rarity_color=rarity['color'])
new_rarity.save()

GAE - Model not saving while another model doing something similar does?

I'm having trouble getting a model to save (or be put()) correctly. The interesting part is that a model doing a very similar save before it works. Below are the relevant parts of the code. At the two logging points the first correctly returns the email of the user. However, the second one results in the error AttributeError: 'NoneType' object has no attribute 'c_user'. Obviously the setting and un-setting of the variables in this is not the correct way to do things, I've just added these to hunt down the problem to discover that the model isn't being saved. Any suggestions? Thank you much!
class Source(db.Model):
current_user = db.UserProperty()
class SourceMember(db.Model):
c_user = db.UserProperty()
x_position = db.IntegerProperty()
y_position = db.IntegerProperty()
...
user = users.get_current_user()
if user:
source_key = self.request.get('g')
if not source_key:
source_key = user.user_id()
source = Source(key_name = source_key,
current_user = user)
source.put()
else:
source = Source.get_by_key_name(source_key)
source = None
source = Source.get_by_key_name(source_key)
logging.warning(source.current_user)
if source:
sourceMember = SourceMember.get_by_key_name(user.user_id() + source_key)
if not sourceMember:
sourceMember = SourceMember(parent = source.key(),
key_name = user.user_id() + source_key,
c_user = user,
x_position = None,
y_position = None)
sourceMember.put()
sourceMember = None
sourceMember = SourceMember.get_by_key_name(user.user_id() + source_key)
logging.warning(sourceMember.c_user)
When you create the SourceMember you're giving it a parent, but then when you get it, the parent is missing. Source doesn't have a parent, so getting it just from its id works.

Retrieving a set of model objects that are indirectly related

I have 4 models:
class TransitLine(models.Model):
name = models.CharField(max_length=32)
class Stop(models.Model):
line = models.ForeignKey(TransitLine, related_name='stops')
class ExitType(models.Model):
name = models.CharField(max_length=32)
people_limit = models.PositiveSmallIntegerField()
class Exits(models.Model):
TOKEN_BOOTH = 0
GATE = 1
REVOLVING_DOOR = 2
EXIT_TYPES = (
(TOKEN_BOOTH, 'Token booth'),
(GATE, 'Gate'),
(REVOLVING_DOOR, 'Revolving door'),
)
name = models.CharField(max_length=32)
stop = models.ForeignKey(Stop, related_name='exits')
type = models.ForeignKey(ExitType, related_name='exits')
I have one TransitLine object. I want to retrieve all the unique ExitType objects that are related to the Stop objects of the TransitLine (that was a mouth full).
Some semi-pseudo code of what I want to do:
tl = TransitLine.objects.get(id=1)
exit_types = []
for s in tl.stops:
exit_types.append([e.type for e in s.exits])
unique_exit_types = list(set(exit_types))
Obviously, prefer to do this in one QuerySet call. Any suggestions?
I would try something like this:
ExitType.objects.filter(exits__stop__line=line).distinct()

Categories