Since `user/course_id` is enough to identify the `AnonymousUsers` row, you should be using `get_or_create(defaults={'anonymous_user_id': _unique_id_for_user(user, course_id)}, user=user, course_id=course_id)`.