Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
6367 มุมมอง

Hiii,

I am a beginner in python programming, how do you calculate the value of the one2many field? I have tried various kinds of references but still not resolved.

this is my code:

    parking_log_ids=fields.One2many('parking_log','parking_log_id',string='Log Parkir', ondelete='cascade')

class ParkingLog(models.Model):
    _name='parking_log'
    _rec_name='vehicle_id'
    

    vehicle_id=fields.Many2one('vehicle',string='Nomor Kendaraan')
    vehicle_type_id=fields.Char(string='Jenis Kendaraan', related='vehicle_id.vehicle_type.name')
    start_time=fields.Datetime(string='Masuk', default=fields.Datetime.now, required='True', readonly='True')
    end_time=fields.Datetime(string='Keluar' ,states={'in' : [('readonly', 'True')], 'paid' : [('readonly', 'True')]})
    rate_id=fields.Integer('Tarif / Jam', related='vehicle_id.vehicle_type.billing')

the value will always be updated as the value in the one2many field increases

and this is an image that shows the value that I will total (but in the picture the value is still 0) :
https://drive.google.com/file/d/1ecUEU9-UvyJcUdnzaXD5sHw7KrVkhZEq/view?usp=sharing

thank you in advance :D

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi Fazryan,

Why don't you make a field that is computed and which counts the amount of records you have on the one2many field?

total_blaya_parkir = fields.Integer("Total blaya parkir", compute="_compute_blaya_total")

@api.multi def _compute_blaya_total(self): self.total_blaya_parkir = len(self.parking_log_ids)

The result will be the total of lines that are set on the parking_log_ids field.

Regards,
Yenthe

อวตาร
ละทิ้ง
ผู้เขียน

Hai Yenthe.

if that's what I input, in the field of one2many. I can't 'add items'

ผู้เขียน

and the plan for the one2many field will be related to another field of type monetary float + widget (say field B), and the value will always be updated as the value in the one2many field increases

ผู้เขียน

errata, Yenthe Your code can be implemented without error. but how do you calculate the total value in that field? because now it only counts the number of fields, not the number of values, thank you

ผู้เขียน

cause float/int cant use len()

Related Posts ตอบกลับ มุมมอง กิจกรรม
2
ส.ค. 25
12021
5
ก.ย. 20
6316
Filter field domain one2many to many2one แก้ไขแล้ว
1
ธ.ค. 19
9735
4
ม.ค. 19
9421
2
ก.ค. 18
14558