0

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 !

Medya Gh
  • 4,563
  • 5
  • 23
  • 35

1 Answers1

0

To anyone who has same problem, I figured out I had to use Atomic Update() instead of Save() because the save() wouldn't block till it is done and my view function will ask for the object before it was saved.

so bottom line is, Save is Evil (mostly). just use atomic update !

like this

User.objects(id=user.id).update(set__update_at=datetime.datetime.utcnow(), push__friend_list="ss")

Medya Gh
  • 4,563
  • 5
  • 23
  • 35