Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
8998 Lượt xem

I have a model 'media.playlist':

class media_playlist(models.Model):

      _name = 'media.playlist'

      start = fields.Datetime('Start', required=True, help="Start Time", store=True)

      stop = fields.Datetime('Stop', required=True, compute='get_stop', store=True, readonly=False, help="End Time")

      @api.depends('start')

      def get_stop(self):

          from datetime import datetime, timedelta

          for p in self:

              if p.start:

                  p_start_date_obj = datetime.strptime(p.start, "%Y-%m-%d %H:%M:%S")

                  p.stop = p_start_date_obj + timedelta(hours=4)

I have a tree and a form view for this model and it all works ok and on record creation both fields get stored into a database using UTC timezone (which is the expected Odoo behavior).

Now, from my own javascript web view I somehow get the wrong values when I do the RPC call:

RPC.query({

    model: 'media.playlist',

    method: 'search_read',

    args: [[['id','=',1],], ['id','start','stop']],

}).then(function (results) { 

    var playlist_view = new Playlist(self, {'playlist': result[0]});

    playlist_view.appendTo(self.$el.find('#media_panel'));

});

A simple template to display the "Playlist" view:

<t t-name="Playlists">

     <div>

        <h4>Playlist</h4>

        <t t-esc="widget.playlist['start']">

        <t t-esc="widget.playlist['stop']">

     </div>

</t>

So my problem is that these two fields (start & stop) get displayed in UTC (the same tz as stored in db). So what am I doing wrong? It works ok on Odoo tree and form views I created using xml, but not on my custom javascript views.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

On odoo views, they convert the datetime according to users timezone.  See my answer here . But while doing the rpc calls you will get the exact value stored in db. so you can convert it to the users timezone from js using moment.js.

eg:

DOnt forget to include  moment.js lib in your backend assets.

https://momentjs.com/

var tz = this.dataset.context.tz
var start = moment.tz(date_begin, "YYYY-MM-DD HH:mm:ss").tz(tz)

Refer this answer also

https://www.odoo.com/forum/help-1/question/time-zone-problem-in-odoo-118905


Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

I would comment on your answer but I don't have enough karma. This forum is terrible.

I was able to get the user's timezone like this:

var session = require('web.session');
var utimezone = session.user_context['tz'];

So no need to do another RPC call, and feed data to views.

But that still didn't move me any forward.. Looks like my moment.js works differently than yours.

TypeError: dict.moment.tz is not a function

This is the error I'm getting with the 2nd line of your code.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 8 16
5160
0
thg 4 16
25783
0
thg 3 15
4389
1
thg 3 15
10226
2
thg 10 19
9933