mongoengine: save doesn't work for datetime consistent with other fields.

I am new to mongoengine, but this doesn’t make any sense to me, that when I call my my_update() function, the user’s updated_at field doesnt get updated but other fields do. here is my model:

class User(db.Document):
    username = db.StringField(required=True, unique=True, max_length=20)
    created_at = db.DateTimeField(default=datetime.datetime.utcnow())
    updated_at = db.DateTimeField()
    friend_list = ListField(StringField(max_length=100))

when I do a save, it saves the new friend_list correctly but it keeps the old updated_at field, and that one will never get updated again.

def my_update(user_id):
    form = UserForm()
    user = User.objects.get_or_404(id=user_id)
    user.friend_list = insert_random_data()
    user.updated_at = datetime.datetime.utcnow()
    user.save()
    return users = User.objects.order_by('-updated_at', '-created_at')

so if I run my_update a few times, it will update the friend_list each time, but the update_at field keeps staying the same !!! and I have no idea. I am really curious why is it behaving like this !

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 27

Most upvoted comments

just remember use no_cache()

users = User.objects.order_by('-updated_at', '-created_at').no_cache()

for the future reference, in case of memory friendly.

def my_update(user_id):
    ....
    User.objects(id=user_id).update(set__update_at=datetime.datetime.now())